![wpf treeview context menu example wpf treeview context menu example](https://www.syncfusion.com/products/wpf/control/images/tree-view/wpf-treeview-metro-style.png)
Why can’t the same be applied for right click? Right click on a treeview is such a common UI event, that users will benefit from it. SelectedNode property of a treeview is set whenever a left click occurs. When PreviewMouseRightButtonDown fires on the visual part of the node (in this sample a TextBlock) the IsSelected property on the view-model is set to true. This way your menu items will look like this:
![wpf treeview context menu example wpf treeview context menu example](https://blog.ctglobalservices.com/wp-content/uploads/old/WPF_TreeView_Example_1.png)
0 Create custom tree view with a dynamic context menu C WPF. For more information, see the ItemsControl class. 1 Adding context menu to tree view by XAML only. ContextMenu is an ItemsControl, which means it can contain a collection of objects of any type (such as string, image, or panel). 4 Adding context menu to leaf node in TreeView WPF. Check if the click was a right click and then use the TreeView control’s GetNodeAt API to get the node which was clicked at. Adding TreeView in WPF with context menu in subitems. The most popular solution is to handle the MouseDown event. So, we need to have some workaround for this. In TreeView control, SelectedNode property is set whenever left click occurs. Adding context menu to tree view by XAML only. Adding TreeView in WPF with context menu in subitems. The sample should help you to fix your problem. A TreeView consists of a hierarchy of TreeViewItem controls, which can contain simple text strings and also more complex content, such as Button controls or a StackPanel with embedded content. Below I have shared a sample app which has a Rename Context Menu. When the user right clicks on the TreeView control, get the node on which the right click was performed. This example shows how to create simple or complex TreeView controls. This may not be required in your scenario though.There is a TreeView control with n number of nodes. Incidentally for TreeViews it is common to use a HierarchicalDataTemplate rather than a plain DataTemplate because this allows for multiple levels of items via the HierarchicalDataTemplate.ItemsSource property. So you should be able to just change your DataTemplate as follows: Integrating the ContextMenu shouldn't be an issue because TextBlock has a ContextMenu property too. Therefore, using a TextBlock instead of a TreeViewItem should cure this.
![wpf treeview context menu example wpf treeview context menu example](https://www.syncfusion.com/products/wpf/control/images/tree-view/wpf-treeview-checkbox-nodes.png)
I suspect the use of TreeViewItem rather than TextBlock causes the excessive indenting because you have a (manually created) TreeViewItem in your DataTemplate (which incurs one level of indent) inside another (automatic) TreeViewItem (which incurs another level of indent). That's why the samples you've found use TextBlocks instead of TreeViewItems in the DataTemplate. You would not normally create a DataTemplate containing a TreeViewItem, because the binding infrastructure will be creating the TreeViewItem for you - all your DataTemplate needs to do is specify what should be displayed as the content of the TreeViewItem. The TreeView should have a context menu, which is specific for each object type.īut with this markup the behavior is as intended, but the child items (the rooms) are indented too much.Īnyway all the bining samples I could find use TextBlock instead of TreeViewItem in the DataTemplate, but wonder how to integrate the ContextMenu there. I'm pretty new to Xaml and need some advise.Ī TreeView should be bound to a hierarchical object structure.