Is it possible to have dynamic context menu for different nodes of the diagram i.e. if I add circle from toolbox and right lick; it shows different menu items to the one if I would have added square.
Thanks Rob, this is exactly I was looking for.
Hi Abs,
I think a better approach would be to attach the XamContextMenu directly to the DiagramNode element instead of the whole XamDiagram control and then in XAML specify what menu items you want shown in the menu. Your sample has a Style for DiagramNode called familyMemberNodeStyle so you can attach a setter there that creates the XamContextMenu. You can then either specify a Click event handler or a Command. If you use a command you can bind to your viewmodel by going through the XamContextMenu.PlacementTargetResolved property. It would look something like this:
<Setter Property="ig:ContextMenuService.Manager"> <Setter.Value> <ig:ContextMenuManager> <ig:ContextMenuManager.ContextMenu> <!-- need to change the foreground color because DiagramNode uses white foreground and this will propagate to the context menu--> <ig:XamContextMenu Foreground="Black"> <ig:XamMenuItem Header="header 1" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ig:XamContextMenu}}, Path=PlacementTargetResolved.DataContext.MyMenuCommand}"/> </ig:XamContextMenu> </ig:ContextMenuManager.ContextMenu> </ig:ContextMenuManager> </Setter.Value></Setter>
In the above example code, PlacementTargetResolved will be the DiagramNode and DataContext will be the FamilyTreeViewModel class in your sample. Then inside this view model you can have an ICommand that executes when the context menu item is clicked and perform some logic there.
I have modified an existing sample which creates a dynamic context menu item based on type of node, what is the best practice to add handlers for the added menu items?