views:

229

answers:

2

I need OnDragEnter event for every cell on my WPF Datagrid. I tried this :

<ControlTemplate TargetType="{x:Type my:DataGridCell}" x:Key="RowTemplate">
    <ContentPresenter DragEnter="ContentPresenter_DragEnter" >
    </ContentPresenter>
</ControlTemplate>

But doesn't seem to work. Any ideas people?


Edit: Thanks for the responses, i realized that i was facing another problem though : My real problem was that the TextBox control always marks drag and drop events as handled so even if you set AllowDrop="True" it will look like AllowDrop is not working. It is not a bug, this behavior is actually by design.

I used the Preview Events to anticipate to this, and be able to handle D'n'D events.

<TextBox
    AllowDrop="True"
    PreviewDragEnter="TextBox_PreviewDragOver" 
    PreviewDragOver="TextBox_PreviewDragOver"  
    PreviewDrop="TextBox_PreviewDrop">
<TextBox/>

Hope this helps.
I am marking your responses as answers as they were accurate regarding the initial question.

+1  A: 

I'm pretty sure you need to set the AllowDrop property to true on the cell. Without this set to true, the element won't participate in the drag/drop events.

Abe Heidebrecht
+2  A: 

You are close.

You need to set AllowDrop, and you need to set it at or below the level you set the event handler. For example:

<ControlTemplate TargetType="{x:Type my:DataGridCell}" x:Key="RowTemplate"> 
  <ContentPresenter DragEnter="ContentPresenter_DragEnter" AllowDrop="true"> 
  </ContentPresenter> 
</ControlTemplate>

Drag/drop events are only sent to UIElements that have AllowDrop="true". From there they bubble up the tree until they are handled.

Note that you can add the DragEnter handler on the DataGrid itself instead of on each cell, but if you want information on which cell is the drop target you should still set AllowDrop="true" at the cell level.

Ray Burns