Drag & Drop in Treeview

前端 未结 1 473
[愿得一人]
[愿得一人] 2020-12-29 07:20

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

相关标签:
1条回答
  • 2020-12-29 08:05

    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;
    }
    
    0 讨论(0)
提交回复
热议问题