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
I have been looking into your requirements and I am not sure what your scenario is. Regarding the selection of a record using interactions could be a solution. I have followed the shared link in order to understand your scenario better and was not able to replicate the same as I am not sure how you use the ContentControl in this case. Could you please modify the sample I have attached before or provide another one that shows the approach? Thank you.
I've included a sample project. When using the ContentControl control the OnInsertNewRow method does not get reached. It works when not using ContentControl. Also, if a row is not selected menuItem paramter in the OnInsertNewRow method is null. How can a row be selected on right click in the data grid so the user does not need first select the row by left clicking on it then right click to get the context menu?
I really appreciate you looking into this for me.
I have been looking into your sample and I modified it, so now it works as you want. Basically I changed the Binding of the MenuItem's Command and I also used the MenuItem to get the ContextMenu. Once I got it I was able to get the clicked Record by using ContextMenu's PlacementTarget Property. Please let me know if this helps you or you have further questions on this matter.
Looking forward for your reply.
Hello, thank's for taking the time to look at my code. Your example worked but I noticed that the menuItem in the OnInsertNewRow(object menuItem) method was null every time I selected something from the context menu. I was able to rectify this by adding CommandParameter="{Binding RelativeSource={RelativeSource Self}}" to the <MenuItem> tag.
Thanks Again!
Thank you for your feedback. I am glad that you resolved your issue and I believe that other community members may benefit from this as well.
Thanks again.