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); }
It works. Thanks.
The problem though is that if the row is not selected grid.SelectedItems.Records[0].Index throws an exception.
So how do I select a row on right mouse click in the data grid? There is a solution here http://www.infragistics.com/community/forums/t/50707.aspx already but that involves handlers. I would like a solution using bindings and properties that keeps my UI decoupled from my view model.
I'm using <ContentControl Content="{Binding SomeViewModel}" /> to change out the content on button clicks. Here is a post that I initiated on this topic. It give you an example of what I'm doing: http://www.infragistics.com/community/forums/t/91690.aspx
The problem is that the solution you provided me above does not work if I use it in conjunction with the <ContentControl> control. The event does not fire. How can I resolve this issue? Thanks