Hello.
How can I prevent the context menu from appearing based on the index the user click on in xamdatagrid? I imagine that I would use a data trigger but how do I get the right-clicked on record's index in xaml?
<ContextMenu.Style> <Style TargetType="{x:Type ContextMenu}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=PlacementTarget.Record}" Value="-1"> <Setter Property="Visibility" Value="Hidden"/> </DataTrigger> </Style.Triggers> </Style> </ContextMenu.Style>
That answered my question. And thanks for the detailed explanation.
Hello KrisVice,
Just checking in, did you still require assistance on this matter?
Sincerely,AndrewAssociate DeveloperInfragistics Inc.www.infragistics.com/support
I have been looking into your sample, and the current binding in the DataTrigger for the CellValuePresenter will not work, as you are binding to the data context of the CellValuePresenter and looking for an IsVisible property. The data context of the CellValuePresenter is the DataRecord, and since there is no IsVisible property on the DataRecord, this binding will fail. You can see this by looking at the output window in Visual Studio, as you will have a binding error for each of the cells in view on startup because of this binding.
Instead, you will need to get the data context of the grid in which the CellValuePresenter sits in. So, rather than writing a Trigger in a style for CellValuePresenter, I would recommend that you write it in a Style for your ContextMenu that you are applying to the CellValuePresenter. The ContextMenu has a PlacementTarget property, and when opened, this will be the CellValuePresenter. So, if you were to use the following DataTrigger in a style for your ContextMenu, you could hide it based on a property:
<DataTrigger Binding={Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.DataPresenter.DataContext.IsVisible} Value=False />
Then, you can set the Visibility of the ContextMenu directly in the style for the context menu. Breaking down the path of that binding, PlacementTarget is the CellValuePresenter, DataPresenter is the XamDataGrid, DataContext is the ViewModel, and the IsVisible property is on the ViewModel.
I have attached an updated version of the sample you sent to demonstrate.
Please let me know if you have any other questions or concerns on this matter.
Andrew, although I solved my previous problem I'm having another. I simply want to hide the context menu based on whether a property is true or false. I've got it to work on other controls like a text box but it's not working on the xamDataGrid. I've attached a sample project. Can you please take a look? Thanks.
Yes, that would work as well. I am glad you were able to find a way to solve this issue you were having.