David Brunelle
David Brunelle

Reputation: 6430

How to determine which row is pointed at when drop

I have a datagrid in WPF in which I can drag and drop stuff in it. When I drop my stuff, I would like to know which row it is dropped in, not just the grid itself.

is there a way to know this and if yes how ?

Thanks

Upvotes: 1

Views: 391

Answers (1)

Davide Piras
Davide Piras

Reputation: 44595

have a look here: WPF drag and drop to DataGrid

In general you should check the target item when you are either dragging over (to enable/disable operations) or dropping (to finalize/execute the operation), for example:

  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;
         }
     }

Upvotes: 1

Related Questions