Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF中的拖放_Wpf_Drag And Drop_Mouseevent - Fatal编程技术网

WPF中的拖放

WPF中的拖放,wpf,drag-and-drop,mouseevent,Wpf,Drag And Drop,Mouseevent,我有一个WPF应用程序的拖放实现。。。 每当我在网格上拖动树项目时,该网格的DragDrop事件会对其进行处理,但每次它被触发两次原因是什么 下面是在TreeView上实现拖放的代码: void treeViewGroups_MouseMove(object sender, MouseEventArgs e) { try { if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed) {

我有一个WPF应用程序的拖放实现。。。 每当我在
网格
上拖动树项目时,该
网格
DragDrop
事件会对其进行处理,但每次它被触发两次原因是什么

下面是在
TreeView
上实现拖放的代码:

 void treeViewGroups_MouseMove(object sender, MouseEventArgs e)
 {
   try
   {
     if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
     {
        Point position = e.GetPosition(null);
        if (Math.Abs(position.X - this.startPoint.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(position.Y - this.startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
        {
          DataRowView treeViewItem = this.treeViewGroups.SelectedItem as DataRowView;
          if (treeViewItem != null)
          if ((treeViewItem.Row.Table.TableName == "TableGroup"))
          {
             ViewTaxSCConstants.dragElement = treeViewItem;
             Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new System.Threading.ParameterizedThreadStart(DoDragDrop), treeViewItem);                                
           }
        }
     }
}

我认为这是一个很好的拖放方法

一个很好的方法,为达格和下降解释如下

将拖动检测为MouseMove和MouseLeftButtonDown的组合

找到要拖动的数据并创建包含格式、数据和允许效果的DataObject

通过调用DoDragDrop()启动拖动

对于要允许删除的元素,将AllowDrop属性设置为True

将处理程序注册到DragEnter事件,以检测拖放位置上的拖动。通过调用事件args上的GetDataPresent()检查格式和数据。如果可以删除数据,请在事件参数上设置Effect属性以显示相应的鼠标光标

当用户释放鼠标按钮时,将调用DragDrop事件。通过对事件args中提供的数据对象调用GetData()方法来获取数据


您可以找到完整的文章

我遇到了几乎相同的问题:我在MouseMove上启动了drag事件,在某些TreeViewItems上启动了drop事件。在第一次触发drop事件后,它将再次触发,但目标将是另一个元素(在我的例子中,是我的目标的父元素)

为了解决这个问题,我必须在Drop事件中设置
e.Handled=true