I am using a SL XamDataTree and wish to bind a context menu to a command in my ViewModel (MVVM).
The tree is defined as follows:
<ig:XamDataTree x:Name="xamTree" AllowDrop="True" IsDropTarget="True" AllowDragDropCopy="False" Grid.Row="0" ItemsSource="{Binding RootFolderContactsNode.SubItemsSorted}" MaxHeight="550" NodeDragDrop="xamTree_NodeDragDrop" Background="Transparent"> <ig:XamDataTree.GlobalNodeLayouts> <ig:NodeLayout x:Name="NL" Key="SubItemsSorted" DisplayMemberPath="Name" TargetTypeName="FolderContactTreeNode" IsDraggable="True" IsDropTarget="True"> <ig:NodeLayout.ItemTemplate> <DataTemplate> <StackPanel> <ig:ContextMenuService.Manager> <ig:ContextMenuManager OpenMode="None"> <ig:ContextMenuManager.ContextMenu> <ig:XamContextMenu Name="cmuFolder" > <ig:XamMenuItem Header="Edit"> <ig:Commanding.Command> <ig:XamContextMenuCommandSource EventName="Click" Command="{Binding DataContext.CommandEditFolderOrContact, ElementName=LayoutRoot}" Parameter="{Binding}"/> </ig:Commanding.Command> </ig:XamMenuItem> </ig:XamContextMenu> </ig:ContextMenuManager.ContextMenu> </ig:ContextMenuManager> </ig:ContextMenuService.Manager> <StackPanel Orientation="Horizontal"> <Image Source="{Binding Data.Image}" Width="12" Height="12"/> <TextBlock Text="{Binding Data.Name}" Margin="2.5,0,0,0"/> </StackPanel> </StackPanel> </DataTemplate> </ig:NodeLayout.ItemTemplate> </ig:NodeLayout> </ig:XamDataTree.GlobalNodeLayouts> </ig:XamDataTree>
I keep getting an unhandled exception because of the Command parameter in the <ig:XamContextMenuCommandSource/> element. 'Command' does not show up in intellisense, but is in the documentation (http://help.infragistics.com/Help/NetAdvantage/Silverlight/2012.1/CLR4.0/html/InfragisticsSL5.Controls.Menus.XamMenu.v12.1~Infragistics.Controls.Menus.XamContextMenuCommandSource_members.html). Any help in how to use Commanding correctly on a ContextMenu in a xamDataTree would be appreciated.
Thank you,
Bill
Hello Bill,
Thank you for your post. I have been looking into it and I suggest you use see this forum thread:
http://es.infragistics.com/community/forums/t/57597.aspx
where a similar question is discussed. Please let me know if this helps you or you need further assistance on this matter.
Looking forward for your reply.
Thanks for the reply. I have already tried these. The first Suggestion I don't see working for us from a design perspective, We are Using PRISM for MVVM.
So I tried the CommandBehavior, but the command is not being set. Does that have to do with the xamContextMenu not being part of the Visual tree, as I have read in other posts? Or do I have to do something special because it is in NodeLayout.ItemTemplate?
<ig:NodeLayout.ItemTemplate> <DataTemplate> <StackPanel> <ig:ContextMenuService.Manager> <ig:ContextMenuManager> <ig:ContextMenuManager.ContextMenu> <ig:XamContextMenu Name="cmuFolder"> <ig:XamMenuItem Header="Edit" x:Name="cmuEdit"> <i:Interaction.Behaviors> <infra:XamMenuItemCommandBehavior Command="{Binding CommandEditFolderOrContact}" CommandParameter="{Binding Data}"/> </i:Interaction.Behaviors> </ig:XamMenuItem> </ig:XamContextMenu> </ig:ContextMenuManager.ContextMenu> </ig:ContextMenuManager> </ig:ContextMenuService.Manager> <StackPanel Orientation="Horizontal"> <CheckBox IsChecked="{Binding Data.IsSelected, Mode=TwoWay}" Margin="0,0,2.5,0"/> <Image Source="{Binding Data.Image}" Width="12" Height="12"/> <TextBlock Text="{Binding Data.Name}" Margin="2.5,0,0,0"/> </StackPanel> </StackPanel> </DataTemplate> </ig:NodeLayout.ItemTemplate>
Hello,
You are right that the XamContextMenu is not part of the Visual Tree, because it is a Popup, so the approach Matt suggested seems to be the best way to achieve the thing you want.