GitXplorerGitXplorer
p

gong-wpf-dragdrop-file-sample

public
11 stars
1 forks
1 issues

Commits

List of commits on branch main.
Verified
13b3f05a34f6309f932fec1d8faa03a280d7ddc4

Create FUNDING.yml

ppunker76 committed 3 years ago
Unverified
65022050d182bfd68ef3142790d12d73ab6166dd

docs: add missing link to simon

ppunker76 committed 3 years ago
Unverified
733949534501c141b7df1bb7fba193463352cf48

docs: update readme with snippets

ppunker76 committed 3 years ago
Unverified
3bb53904d47ee32c60b938cabece30e38d119a77

add file drag drop sample

ppunker76 committed 3 years ago
Verified
d688c698f7deffdc0da0984efc9393192dd4aa7c

Initial commit

ppunker76 committed 3 years ago

README

The README file for this repository.

File drag&drop

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>

Drop files

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);
    }
}

snippet source | anchor

Drag files

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;
}

snippet source | anchor

In action

Credits

README generated with MarkdownSnippets by @SimonCropp