Hi,
I have a context menu on a xam atatree which is generated from another xamdatatree.
I want to show / hide certain menu items depending on whether the tree was right-clicked or the node level. Also, how to find out which node was clicked? Does the node have to be selected to get the node data from right click?
Please see sample project attached. It is not perfect, I am still getting used to the Tree control, but you will get the idea. I have added comments in the code behind.
Thanks
Hello Mansi,
Thank you for your support request.
I have reviewed the sample project you have attached. About your requirement for which node was clicked, I suggest you to define the ContextMenu to XamDataTreeNodeControl directly instead to XamDataTree. You can achieve this by creating a simple style in the Resources of the tree, for example:
<ig:XamDataTree.Resources> <Style TargetType="{x:Type ig:XamDataTreeNodeControl}"> <Setter Property="ContextMenu" > <Setter.Value> <ContextMenu > <MenuItem Header="Click" Visibility="Visible"/> </ContextMenu> </Setter.Value> </Setter> </Style> </ig:XamDataTree.Resources>
In the code behind you can register the events you wish fot this menu, you can achive this easy with EventManager class:
EventManager.RegisterClassHandler(GetType(XamDataTreeNodeControl), XamDataTreeNodeControl.ContextMenuOpeningEvent, New ContextMenuEventHandler(AddressOf ContextMenu_Opening))EventManager.RegisterClassHandler(GetType(MenuItem), MenuItem.ClickEvent, New RoutedEventHandler(AddressOf ContextMenu_Clicked))
so in the ContextMenu_Click handler you can find the clicked node througth PlacementTarget property of the menu:
Private Sub ContextMenu_Clicked(sender As Object, e As RoutedEventArgs)
If TypeOf (sender) Is MenuItem Then Dim menuItem As MenuItem = DirectCast(sender, MenuItem) '' which node did I click? Dim clickedNode As XamDataTreeNodeControl = DirectCast(menuItem.Parent, ContextMenu).PlacementTarget MessageBox.Show("You clicked: " + DirectCast(clickedNode.Node.Data, clsMenuItem).Description.ToString()) End If
End Sub
I am not sure that I understand correclty your requirment regarding show/hdie certain menu items. Would you please provide me with more infromation about this?
Please do not hesitate to let me know if you require any futher assistance.
Thanks for the sample.
By show / hide, I mean that if I click a node which has children, then I will see different context menu items, and if it is a child node, then I will see different options in the context menu.
For ex, if I click a parent node, I will see "Expand All", but I won't see this menu item if I click on a child node.