Hi,
How to display context menu in the datagrid rows and pass the selected menuitem header to command parameter ?
I tried the following code but its not working.
<igWpf:XamDataGrid.Resources> <Style TargetType="{x:Type igWpf:DataRecordCellArea}"> <Setter Property="ContextMenu"> <Setter.Value> <ContextMenu ItemsSource="{Binding ContextMenuDatas}"> <ContextMenu.ItemContainerStyle> <Style TargetType="{x:Type MenuItem}"> <Setter Property="Header" Value="{Binding Name}" /> <Setter Property="Command" Value="{Binding DataContext.TestCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" /> <Setter Property="CommandParameter" Value="{Binding Header, RelativeSource={RelativeSource Self}}" /> </Style> </ContextMenu.ItemContainerStyle> </ContextMenu> </Setter.Value> </Setter> </Style> </igWpf:XamDataGrid.Resources>
Hello Ramesh,Thank you for your feedback. I am glad to know that I was able to help you achieve the functionality you were looking for. I believe this thread can help other people looking for a similar solution.If you require any further assistance on this matter, please do not hesitate to ask.
Thanks! Its working perfectly
Hello Ramesh, 1) In order to display the items of a ContextMenu for the DataRecordCellArea by binding the ItemsSource property to a collection of custom objects, an approach I can suggest you is to create a local xaml instance of the collection and reference it directly. The reason for not binding directly to a property is because the ContextMenu is a popup element and does not contain the Window and the XamDataGrid in it's visual tree and we will not be able to bind for the DataContext of a parent control.XAML: <vm:ContextMenuItems x:Key="contextMenuItems" />C#: public class MyMenuItem{ public string MyName { get; set; } public MyMenuItem(string myName) { this.MyName = myName; }} public class ContextMenuItems : ObservableCollection<MyMenuItem>{ public ContextMenuItems() { this.Add(new MyMenuItem("Copy")); ... }} 2) In order to pass the Header of the selected MenuItem as a CommandParamater, you can do that by setting the Command and CommandParameter properties respectively. <Setter Property="Command" Value="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType=Window}}" /><Setter Property="CommandParameter" Value="{Binding Header, RelativeSource={RelativeSource Self}}" /> I have prepared a sample application with an implementation of the above behavior.If you require any further assistance on this matter, please do not hesitate to ask.