-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomFileDragSource.cs
39 lines (35 loc) · 1.46 KB
/
CustomFileDragSource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using GongSolutions.Wpf.DragDrop;
namespace DragHandlerSample
{
public class CustomFileDragSource : DefaultDragHandler
{
public override bool CanStartDrag(IDragInfo dragInfo)
{
// Check that the control which started the drag is a label and the content is a string.
// Can be changed to the desired control type
return (dragInfo.VisualSource is Label label && label.Content is string);
}
public override void StartDrag(IDragInfo dragInfo)
{
// Get the control that started the drag and the filename from the label
Label sourceControl = dragInfo.VisualSource as Label;
if (sourceControl == null || !(sourceControl.Content is string fileName))
return;
// Get the path of TextFile.txt in the output directory
string folder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string path = Path.Combine(folder, fileName);
// Drag the TextFile.txt in the output directory
dragInfo.DataFormat = DataFormats.GetDataFormat(DataFormats.FileDrop);
dragInfo.Data = new string[] { path };
dragInfo.Effects = DragDropEffects.Copy;
}
}
}