How to override default cut/copy/paste context menu in cells

Feb 5, 2009 at 6:27 PM
I have a context menu on my DataGrid that works just fine when a cell is not being edited. However, if a cell is being edited, its context menu shows a different menu with cut, copy and paste on it. I would like the same context menu shown

I have tried a few different things that did not work. One was to define the context menu as a resource and set the context menu on the DataGridCell via the CellStyle property.



<ContextMenu x:Key="dataGridContextMenu">
<MenuItem Header="Paste data" Click="PasteFromClipboard"/>




<Style TargetType="{x:Type Controls:DataGridCell}">
<Setter Property="ContextMenu" Value="{StaticResource dataGridContextMenu}">

Does anyone have any suggestions on how this can be achieved?




Feb 5, 2009 at 6:50 PM
I found a way to achieve this, although it seems a little clumsy.

In the PreviewMouseRightButtonDown event handler, add code as follows:

ContextMenu.IsOpen = true;
e.Handled =


This works OK for right clicks,  but does not handle the case where the user presses the context menu key on the keyboard but it seems that not many people use that key anyway.

If anyone else has a better solution, please share!



Feb 6, 2009 at 7:33 AM
I believe the recommended approach would be to retemplate the editing state control that exists inside the DataGridCell on edit, and replace the context menu there.