I\'m trying to drag and drop files in my treeview but I have no idea why it\'s breaking down if I run it and try dragging a file.
The code below is what I tried. Ple
This article is very helpful. Drag drop wpf
This code may be of use to you as well.
Point _startPoint;
bool _IsDragging = false;
void TemplateTreeView_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed ||
e.RightButton == MouseButtonState.Pressed && !_IsDragging)
{
Point position = e.GetPosition(null);
if (Math.Abs(position.X - _startPoint.X) >
SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(position.Y - _startPoint.Y) >
SystemParameters.MinimumVerticalDragDistance)
{
StartDrag(e);
}
}
}
void TemplateTreeView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(null);
}
private void StartDrag(MouseEventArgs e)
{
_IsDragging = true;
object temp = this.TemplateTreeView.SelectedItem;
DataObject data = null;
data = new DataObject("inadt", temp);
if (data != null)
{
DragDropEffects dde = DragDropEffects.Move;
if (e.RightButton == MouseButtonState.Pressed)
{
dde = DragDropEffects.All;
}
DragDropEffects de = DragDrop.DoDragDrop(this.TemplateTreeView, data, dde);
}
_IsDragging = false;
}