Hello,
I have a the following code (Code #1) attached to a XamDataGrid:
<ig:ContextMenuManager> <ig:ContextMenuManager.ContextMenu> <ig:XamContextMenu> <ig:XamMenuItem Header="Edit"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <i:InvokeCommandAction Command="{Binding Path=MyCommand}" /> </i:EventTrigger> </i:Interaction.Triggers> </ig:XamMenuItem> </ig:XamContextMenu> </ig:ContextMenuManager.ContextMenu></ig:ContextMenuManager>where xmlns:ig="http://schemas.infragistics.com/xaml" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"The command does not work as intended, nothing is being shown.The following code (Code #2) works as intended:<Button Content="Save" Height="20"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <i:InvokeCommandAction Command="{Binding Path=MyCommand}"/> </i:EventTrigger> </i:Interaction.Triggers> </Button>When I change MyCommand in Code #2 to MyCommand2 (which doesn't exist) it tries to find the property MyCommand2 on the DataContextMyCommand does actually exist on the DataContext (as shown on Code #2)The command actually isprivate ICommand _myCommand;public ICommand MyCommand{ get { return _myCommand ?? (_myCommand = new RelayCommand(param => MessageBox.Show("TEST!"))); } }I'm using the MVVM architecture.Why doesn't the Code #1 work? Any ideas?Thank you!
Thank you Curtis! It will be useful for others in the same situation as well.
Since the declaration of the XamContextMenu acts like a DataTemplate in that it is not part of the Visual Tree, it does not have access to the DataContext of the view it is embedded in. The way around this is to instantiate the ViewModel source in the ResourceDictionary of the container. Then you can access this data source directly and apply it to the Command Binding expression directly.
<Window.Resources> <local:MainViewModel x:Key="ViewModelDataSource" /></Window.Resources>....<i:InvokeCommandAction Command="{Binding Path=MyCommand, Source={StaticResource ViewModelDataSource}}"/>
I've attached a sample to demonstrate.
Thank you!
I spoke with development on this issue.
A xamContextMenu isn't in the VisualTree, and thus doesn't partake in the DataContext inheritance. The wpf Contextmenu will work because it is in the visualTree.
To get this to work, your binding would need a Source set to a StaticResource for the command.
Valerie
You're right. It looks like the InvokeCommandAction Behavior fails to work in a XamMenuItem in a XamContextMenu. I've reported this as a bug and it is assigned Case ID: CAS-63623-2YJ4B9.
I apologize for the inconvenience!