如何确定drop时指向哪一行

时间:2011-08-23 19:32:46

标签: .net wpf drag-and-drop

我在WPF中有一个datagrid,我可以在其中拖放内容。当我丢弃我的东西时,我想知道它被放入哪一行,而不仅仅是网格本身。

有没有办法知道这个,如果是的话怎么样?

由于

1 个答案:

答案 0 :(得分:1)

看看这里:WPF drag and drop to DataGrid

通常,在拖动(启用/禁用操作)或删除(完成/执行操作)时应检查目标项,例如:

  private void DataGrid_CheckDropTarget(object sender, DragEventArgs e)
     {
         if (FindVisualParent<DataGridRow>(e.OriginalSource as UIElement) == null)
         {
             e.Effects = DragDropEffects.None;
         }
         e.Handled = true;
     }

     private void myDataGrid_Drop(object sender, DragEventArgs e)
     {
         e.Effects = DragDropEffects.None;
         e.Handled = true;

         // Verify that this is a valid drop and then store the drop target
         DataGridRow container = FindVisualParent<DataGridRow>(e.OriginalSource as UIElement);
         if (container != null)
         {
             _targetItem = container.DataContext;
             e.Effects = DragDropEffects.Move;
         }
     }