Create ContextMenu of WpfTreeView

Hi everybody.
I want to build some sort of browser window using the WpfTreeView. My problem now is, that I wnat to add a ContextMenu (Right click) to the Items, just like the Menus in e.g. the Layout browser. I added a context menu in the .xaml of the usercontrol, but I want to show the ContextMenu only on the childnodes of my rootnode.

So how can I add the Menu only to the childnodes and not to the whole treeview?


<rs:WpfTreeView.ContextMenu>
  <ContextMenu Background="WhiteSmoke">
    <MenuItem Header="Remove" Click="ContextMenuRemove_Click"/>
  </ContextMenu>
</rs:WpfTreeView.ContextMenu>

I don’t think it’s possible to do completely in XAML.
But you could subscribe to the right-click event and display a context menu depending on what is selected. Something like this:

        private void _treeView_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_treeModel.SelectedNodes.Count() == 1) // Or whatever condition
            {
                var ctxMenu = this.FindResource("_ctxMenu1") as ContextMenu;
                ctxMenu.IsOpen = true;
            }
        }

Okay. Just thought their would be a better or more elegant way than that. But than I will make it like this. Thank you.