Articles in this section
Category / Section

How to open different ContextMenu for different levels of Nodes in WPF TreeView?

2 mins read

WPF TreeView (SfTreeView), doesn’t have any direct support to open different context menu based on the level of the nodes in which the context menu is opening. However, you can achieve it by handling SfTreeView.ItemContextMenuOpening event.

this.treeView.ItemContextMenuOpening += OnTreeView_ItemContextMenuOpening;
 
private void OnTreeView_ItemContextMenuOpening(object sender, ItemContextMenuOpeningEventArgs e)
{
    if (e.MenuInfo.Node.Level == 1)
    {
        MenuItem item1 = new MenuItem();
        item1.Header = "Level 1";
        e.ContextMenu.Items.Clear();
        e.ContextMenu.Items.Add(item1);
    }
 
    else if (e.MenuInfo.Node.Level == 0)
    {
        MenuItem item1 = new MenuItem();
        item1.Header = "Level 0";
        e.ContextMenu.Items.Clear();
        e.ContextMenu.Items.Add(item1);
    }
}

Context Menu based on level            

 

Take a moment to peruse the WPF TreeView - ContextMenu documentation, where you can find about contextMenu, with code examples.

View sample in GitHub

 
Conclusion

I hope you enjoyed learning about how to open different ContextMenu for different levels of Nodes in TreeView.

You can refer to our WPF TreeView feature tour page to know about its other groundbreaking feature representations and WPF TreeView documentation and how to quickly get started for configuration specifications.

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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied