C#/WPF

[WPF] 외부파일 Drag & Drop

딸기우유중독 2025. 3. 11. 10:58

 

 

 

xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:behavior="clr-namespace:SampleProject.ViewModel.Behavior"
<Grid AllowDrop="True">
<!--Drag & Drop 할 공간-->
</Grid>
<Grid AllowDrop="True">
    <i:Interaction.Behaviors>
        <behavior:FileDropBehavior DroppedFiles="{Binding DroppedFiles, Mode=TwoWay}"/>
    </i:Interaction.Behaviors>
</Grid>

 

class FileDropBehavior : Behavior<FrameworkElement>
{
    public static readonly DependencyProperty DroppedFilesProperty = DependencyProperty.Register("DroppedFiles", typeof(string[]), typeof(FileDropBehavior), new PropertyMetadata(default(string[])));
    public string[] DroppedFiles
    {
        get { return (string[])GetValue(DroppedFilesProperty); }
        set { SetValue(DroppedFilesProperty, value); }
    }

    protected override void OnAttached()
    {
        AssociatedObject.Drop += AssociatedObjectDrop;
    }

    private void AssociatedObjectDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            DroppedFiles = (string[])e.Data.GetData(DataFormats.FileDrop);
        }
    }

    protected override void OnDetaching()
    {
        AssociatedObject.Drop -= AssociatedObjectDrop;
    }
}

 

 

 

AssociatedObject.DragEnter

DragLeave

DragOver

할 때 

e.Handled = true; 줘야 적용 됨.

 

 

 


변경 된  e 를 적용하기 위해서는

e.Handled를 해야 적용 됨.

ex)

아래 files is null이라서 retrurn 할 경우 

e.Effects = DragDropEffects.None;

적용이 안된 상태로 끝남.

 

 

return 전에 

e.Handled = true;

적용해야 함.


 

https://nonstop-antoine.tistory.com/103

 

[WPF] File Drag & Drop MVVM Pattern using Behavior

외부의 파일을 종종 프로그램 내부로 가져와서 쓸때가 있다. 보통은 File Open Dialog등을 이용하여 가져올수도 있지만 그냥 파일 탐색기에서 여러 파일들을 선택해서 Drag & Drop형태로 가져올 때도

nonstop-antoine.tistory.com

 

 

https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/drag-and-drop-overview?view=netframeworkdesktop-4.8&viewFallbackFrom=netdesktop-9.0

 

Drag and Drop Overview - WPF .NET Framework

Learn about drag-and-drop support in Windows Presentation Foundation applications, which lets users drag objects to a region in the user interface.

learn.microsoft.com

 

 

https://stackoverflow.com/questions/44319258/wpf-drag-drop-none-effect-not-triggering

 

WPF drag drop none effect not triggering

So I have pretty much followed every example I can find on how to set the drag/drop effect on a WPF control to None to show the circle with a line symbol, but to no avail. Here is the code I have ...

stackoverflow.com

 

 

 

 

728x90