Simple File drop sample with gong-wpf-dragdrop.
<ListBox Grid.Row="1"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.DragHandler="{Binding}"
dd:DragDrop.DropHandler="{Binding}"
dd:DragDrop.UseDefaultDragAdorner="True"
ItemsSource="{Binding Files}">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type models:FileModel}">
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding FileName}" FontSize="18" FontWeight="Bold" />
<TextBlock Text="{Binding File}" FontSize="12" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
public void DragOver(IDropInfo dropInfo)
{
if (dropInfo.DragInfo?.VisualSource is null
&& dropInfo.Data is DataObject dataObject
&& dataObject.GetDataPresent(DataFormats.FileDrop)
&& dataObject.ContainsFileDropList())
{
// Note that you can have more than one file.
dropInfo.Effects = dataObject.GetData(DataFormats.FileDrop) is string[] files
&& files.Any(File.Exists)
? DragDropEffects.Copy
: DragDropEffects.None;
}
else
{
GongSolutions.Wpf.DragDrop.DragDrop.DefaultDropHandler.DragOver(dropInfo);
}
}
public void Drop(IDropInfo dropInfo)
{
if (dropInfo.DragInfo?.VisualSource is null
&& dropInfo.Data is DataObject dataObject
&& dataObject.GetDataPresent(DataFormats.FileDrop)
&& dataObject.ContainsFileDropList())
{
// Note that you can have more than one file.
var files = dataObject.GetFileDropList();
foreach (var file in files)
{
this.Files.Add(new FileModel(file));
}
}
else
{
GongSolutions.Wpf.DragDrop.DragDrop.DefaultDropHandler.Drop(dropInfo);
}
}
public void StartDrag(IDragInfo dragInfo)
{
// drag&drop inside the ListBox with control key
if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
{
GongSolutions.Wpf.DragDrop.DragDrop.DefaultDragHandler.StartDrag(dragInfo);
}
else
{
var files = dragInfo.SourceItems.OfType<FileModel>().Select(f => f.File).ToArray();
var dataObject = new DataObject();
var sc = new System.Collections.Specialized.StringCollection();
sc.AddRange(files!);
dataObject.SetFileDropList(sc);
dragInfo.DataObject = dataObject;
dragInfo.Effects = DragDropEffects.Copy;
}
}
public bool CanStartDrag(IDragInfo dragInfo)
{
return true;
}
README generated with MarkdownSnippets by @SimonCropp