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
55
XamDataTree right click selecting node ?
posted

Hi,

I'm trying to find a way to select a node with right-click before opening contextual menu.

Is this possible ?

Thanks for your help

Parents
No Data
Reply
  • 6912
    posted

    hi,

    You can try this code:

    <ig:XamDataTree MouseRightButtonDown="XTree_MouseRightButtonDown"
                    ItemsSource="{Binding Data}">
        <ig:ContextMenuService.Manager>
            <ig:ContextMenuManager>
                <ig:ContextMenuManager.ContextMenu>
                    <ig:XamContextMenu Opening="XamContextMenu_Opening"
                                       Opened="XamContextMenu_Opened">
                        <ig:XamMenuItem Header="Item 1" />
                        <ig:XamMenuItem Header="Item 2" />
                    </ig:XamContextMenu>
                </ig:ContextMenuManager.ContextMenu>
            </ig:ContextMenuManager>
        </ig:ContextMenuService.Manager>
    </ig:XamDataTree>
    private XamDataTreeNodeControl clickedNode;
    
    private void XamContextMenu_Opening(object sender, OpeningEventArgs e)
    {
        var node = e.GetClickedElements<XamDataTreeNodeControl>().FirstOrDefault();
    
        if (node != null
            && node.Node != null
            && node.Node.NodeLayout != null
            && node.Node.NodeLayout.Tree != null)
        {
            clickedNode = node;
    
            var selectedNodes = clickedNode.Node.NodeLayout.Tree.SelectionSettings.SelectedNodes;
            selectedNodes.Clear();
            selectedNodes.Add(clickedNode.Node);
    
            return;
        }
    
        clickedNode = null;
        e.Cancel = true;
    }
    
    private void XamContextMenu_Opened(object sender, OpenedEventArgs e)
    {
        // TODO
    }
    
    private void XTree_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        XamDataTree dataTree = sender as XamDataTree;
        UIElement rootVis = Application.Current.RootVisual;
    
        if (dataTree != null && rootVis != null)
        {
            var nodeControl =
                VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(rootVis), dataTree)
                                .OfType<XamDataTreeNodeControl>()
                                .FirstOrDefault();
    
            if (nodeControl != null && nodeControl.Node != null)
            {
                nodeControl.Node.IsActive = true;
            }
        }
    }

    Hope this helps

Children