Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
290
Context Menu on DataTree
posted

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

DataTreeVBNET.zip
Parents
  • 16495
    Offline posted

    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.

    DataTreeVBNET_modified.zip
Reply Children