How to display different menus for Node and Annotation in WPF Diagram?
In the WPF Diagram, you can display different context menus for the Node and Annotation by using the MouseRightButtonDown event. In this event, you can utilize the FindVisualParent method to identify the View object on which the right-click was performed. Based on this identification, you can generate different menus for Node and Annotation. We have provided the code snippet to achieve this.
Code Snippet :
this.MouseRightButtonDown += MainWindow_MouseRightButtonDown;
private void MainWindow_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
DependencyObject source = e.OriginalSource as DependencyObject;
// Find the Visual Parent for the Annotation and Node.
var annotation = source.FindVisualParent<AnnotationEditor>();
var node = source.FindVisualParent<Node>();
if (annotation != null)
{
if (annotation.DataContext != null && annotation.DataContext is AnnotationEditorViewModel)
{
AnnotationEditorViewModel anno = annotation.DataContext as AnnotationEditorViewModel;
Node parentNode = annotation.FindVisualParent<Node>();
NodeViewModel nodeVM = parentNode.DataContext as NodeViewModel;
nodeVM.Menu = new DiagramMenu();
nodeVM.Menu.MenuItems = new ObservableCollection<DiagramMenuItem>();
DiagramMenuItem EditAnno = new DiagramMenuItem()
{
Content = "Edit Annotation",
Command = new DelegateCommand<object>(OnEditAnnotation),
CommandParameter = anno,
};
(nodeVM.Menu.MenuItems as ICollection<DiagramMenuItem>).Add(EditAnno);
DiagramMenuItem DeleteAnno = new DiagramMenuItem()
{
Content = "Delete Annotation",
Command = new DelegateCommand<object>(OnDeleteAnnotation),
CommandParameter = anno,
};
(nodeVM.Menu.MenuItems as ICollection<DiagramMenuItem>).Add(DeleteAnno);
}
}
else if (node != null)
{
NodeViewModel nodeVM = node.DataContext as NodeViewModel;
nodeVM.Menu = new DiagramMenu();
nodeVM.Menu.MenuItems = new ObservableCollection<DiagramMenuItem>();
DiagramMenuItem EditNode = new DiagramMenuItem()
{
Content = "Edit Node",
Command = new DelegateCommand<object>(OnEditNode),
CommandParameter = nodeVM,
};
(nodeVM.Menu.MenuItems as ICollection<DiagramMenuItem>).Add(EditNode);
DiagramMenuItem DeleteNode = new DiagramMenuItem()
{
Content = "Delete Node",
Command = new DelegateCommand<object>(OnDeleteNode),
CommandParameter = nodeVM,
};
(nodeVM.Menu.MenuItems as ICollection<DiagramMenuItem>).Add(DeleteNode);
}
}
Conclusion
I hope you enjoyed learning how to display different menus for Node and Annotation in WPF Diagram.
You can refer to our WPF Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our WPF Diagram example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!