Hi,
I'm presently using the XamDataGrid and I writing a context menu to allow copying and pasting of a selected row or cell. I've created a context menu in xaaml in the XamDatGrid.Resources and handled the CellValuePresenter ContextMenuOpening event. The context menu appears on a right click as expected. The issue I'm having is that when a contextMenuItem is selected how do you pass down the current CellValuePresenter object in the event hander for the menu item that the menu was invoked from?
Cheers
Gary
Hi Valerie,
Thanks, that seems to work. Before I was saving the last CellValuePresenter in the xaml code behind which I really didn't like. This is the much preferred way.
Hello Gary,
The Context Menu’s PlacementTarget property will contain the CellValuePresenter that was clicked. You can set the ContextMenu’s DataContext to this value and then the DataContext will propagate down to the menu items in the menu. Then you can simply get the CellValuePresenter from the datacontext when the item is clicked. From there, if needed you can further investigate the actual data item clicked. For example:
<igWPF:XamDataGrid.Resources>
<ContextMenu x:Key="ContextMenu" DataContext="{Binding PlacementTarget,RelativeSource={RelativeSource self}}" >
<MenuItem Header="Copy" Click="OnCopyClicked"/>
</ContextMenu>
<Style TargetType="{x:Type igWPF:CellValuePresenter}">
<Setter Property="ContextMenu" Value="{StaticResource ContextMenu}"/>
</Style>
</igWPF:XamDataGrid.Resources>
private void OnCopyClicked(object sender, RoutedEventArgs e)
{
MenuItem item = sender as MenuItem;
CellValuePresenter cvp = (CellValuePresenter) item.DataContext;
var dataItem = ((DataRecord)cvp.DataContext).DataItem;
}
Please let me know if you have any questions,
Sincerely,
Valerie
Software Developer
Infragistics Inc