[英]Drag drop in WPF
我有拖拽實現的WPF應用程序......每當我在Grid
上拖動樹項目時,它都會被該Grid
的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上啟動了拖動事件,並在某些TreeViewItems上有一個drop事件。 在首先觸發drop事件之后,它會再次觸發,但目標將是一個不同的元素(在我的情況下,是我的目標的父元素)。
要解決這個問題,我必須在Drop事件中設置e.Handled = true
。
我認為這是拖放的好方法
解釋darg和drop的好方法
檢測拖動作為MouseMove和MouseLeftButtonDown的組合
找到要拖動的數據並創建包含格式,數據和允許的效果的DataObject。
通過調用DoDragDrop()啟動拖動
在要允許刪除的元素上將AllowDrop屬性設置為True。
將處理程序注冊到DragEnter事件以檢測拖放位置上的拖動。 通過在事件args上調用GetDataPresent()來檢查格式和數據。 如果可以刪除數據,請在事件參數上設置Effect屬性以顯示相應的鼠標光標。
當用戶釋放鼠標按鈕時,將調用DragDrop事件。 通過在事件args中提供的Data對象上調用GetData()方法來獲取數據。
你可以在這里找到完整的文章
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.