简体   繁体   English

在WPF中拖放

[英]Drag drop in WPF

I have WPF application with drag-n- drop inmplementation... Whenever I drag tree item on a Grid it is processed by DragDrop Event of that Grid , but every time it get fired twice what could be the reason? 我有拖拽实现的WPF应用程序......每当我在Grid上拖动树项目时,它都会被该GridDragDrop事件处理,但每次它被解雇两次可能是什么原因?

Below is code for implementing drag drop on a TreeView : 下面是在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);                                
           }
        }
     }
}

I had nearly the same problem: I started the drag event on MouseMove and had a drop event on certain TreeViewItems. 我有几乎相同的问题:我在MouseMove上启动了拖动事件,并在某些TreeViewItems上有一个drop事件。 After the drop event fired first, it'd fire a second time but the target would be a different element (in my case, the parent of the one that was my target). 在首先触发drop事件之后,它会再次触发,但目标将是一个不同的元素(在我的情况下,是我的目标的父元素)。

To solve it, I had to set e.Handled = true in the Drop event. 要解决这个问题,我必须在Drop事件中设置e.Handled = true

I think this is a good method for Drag & Drop 我认为这是拖放的好方法

A good way for darg and drop are explained as 解释darg和drop的好方法

Detect a drag as a combinatination of MouseMove and MouseLeftButtonDown 检测拖动作为MouseMove和MouseLeftButtonDown的组合

Find the data you want to drag and create a DataObject that contains the format, the data and the allowed effects. 找到要拖动的数据并创建包含格式,数据和允许的效果的DataObject。

Initiate the dragging by calling DoDragDrop() 通过调用DoDragDrop()启动拖动

Set the AllowDrop property to True on the elements you want to allow dropping. 在要允许删除的元素上将AllowDrop属性设置为True。

Register a handler to the DragEnter event to detect a dragging over the drop location. 将处理程序注册到DragEnter事件以检测拖放位置上的拖动。 Check the format and the data by calling GetDataPresent() on the event args. 通过在事件args上调用GetDataPresent()来检查格式和数据。 If the data can be dropped, set the Effect property on the event args to display the appropriate mouse cursor. 如果可以删除数据,请在事件参数上设置Effect属性以显示相应的鼠标光标。

When the user releases the mouse button the DragDrop event is called. 当用户释放鼠标按钮时,将调用DragDrop事件。 Get the data by calling the GetData() method on the Data object provided in the event args. 通过在事件args中提供的Data对象上调用GetData()方法来获取数据。

You can find the complete article here 你可以在这里找到完整的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM