Articles in this section
Category / Section

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

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 documentation, where you can find about ContextMenu in SfTreeView, with code examples.

You can download the example from GitHub

 

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