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
thanks.. it works
Hello Rajeev,
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.
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.
If you don't want a reference to the MenuItem in the command, then what you want to do in the command?
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.