Hello. This is what I have:
<igWPF:XamDataGrid DataSource="{Binding MainLogDataGrid}" > <igWPF:XamDataGrid.Resources> <ContextMenu x:Key="RecordContextMenu"> <MenuItem Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type igWPF:XamDataGrid}}, Path=DataContext.InsertCommand}" Header="Open" /> </ContextMenu> <Style TargetType="{x:Type igWPF:CellValuePresenter}"> <Setter Property="ContextMenu" Value="{DynamicResource RecordContextMenu}"></Setter> </Style> </igWPF:XamDataGrid.Resources> </igWPF:XamDataGrid>
In my main view model class:
public RelayCommand<object> InsertCommand {get; private set;}
public MainViewModel(){InsertCommand = new RelayCommand<object>(OnButtonPressMethod);}
public void OnButtonPressMethod(object menuItem) { var item = menuItem as MenuItem; }
menuItem parameter in OnButtonPressMethod is null when I select whatever is in the context menu. I do have some dummy data in my xamDataGrid. Thanks
Hello Kris,
Thank you for the provided code snippet. I have been looking into it and I noticed that you have not bound the CommandParameter property in order to pass the menu item to the command. Here is a code snippet for the same which binds the item on my side:<igWPF:XamDataGrid DataSource="{Binding Collection}" > <igWPF:XamDataGrid.Resources> <ContextMenu x:Key="RecordContextMenu"> <MenuItem Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type igWPF:XamDataGrid}}, Path=DataContext.InsertCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}" Header="Open" /> </ContextMenu> <Style TargetType="{x:Type igWPF:CellValuePresenter}"> <Setter Property="ContextMenu" Value="{DynamicResource RecordContextMenu}"></Setter> </Style> </igWPF:XamDataGrid.Resources> </igWPF:XamDataGrid>Please let me know in case you need additional assistance for the same.
This worked thanks. What I'm really trying to do is to know which item was selected in the context menu so I know whether to say copy or delete a row and what the index of the selected item is in the XamDataGrid. How can I accomplish this? Thanks.
I am really glad that the approach is helpful for you.From your description it seems that you need to pass both the menu item and the XamDataGrid control and operate with them when OnButtonPressMethod is called. You can then pass the grid as a Tag property to the item like this: <ContextMenu x:Key="RecordContextMenu"> <MenuItem Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type igWPF:XamDataGrid}}, Path=DataContext.InsertCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}" Header="Open" Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type igWPF:XamDataGrid}}}"/> </ContextMenu>
public void OnButtonPressMethod(object menuItem) { var item = menuItem as MenuItem; XamDataGrid grid = item.Tag as XamDataGrid; if (grid != null && grid.SelectedItems.Records.Count >0) MessageBox.Show("Index of the first selected record is: " + grid.SelectedItems.Records[0].Index); }
I tried this solution and it works. But I have a question. I feel using menuItem(system.windows) reference for typecasting XamDataGrid tag in View model violates the very basic rule of MVVM design. We cannot bring up view reference inside viewmodel . We are inviting the tight coupling again Could you suggest any MVVM way out for this problem. The commands are written in view models.
Hello Rajeev,
If you don't want a reference to the MenuItem in the command, then what you want to do in the command?
Looking forward for your reply.
I basically want to pass the underlying row details(bond entity) on which user right clicks to perform action using menu item.
You can change the Binding of the CommandParameter like this:
CommandParameter="{Binding RelativeSource={RelativeSource Self},Path=Parent.PlacementTarget.Record.DataItem}"
This way you will get the underlying object as parameter. Please let me know if this helps you or you have further questions on this matter.
thanks.. it works