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
610
xamTree with ContextMenu, selecting node on right click
posted

Hello,

I have a xamTree databound with three levels of nodes. I put a context menu on the tree control. Right now a user can right click a node to pop up the context menu to add a child node. The problem is that unless they first LEFT click the node, then RIGHT click the node for the context menu, I am unable to get the selected node to add the child.

 I tried adding a MouseRightButtonDown to the tree, but I get all different UIElements as the OriginalSource (could be TextBlock or Grid or Image depending on where they right clicked).

How can I select the node on right click before the context menu pops up?

 

Thanks,

-J

Parents
  • 5595
    Verified Answer
    posted

    Hi,

    You could obtain the XamTreeItem in the XamContextMenu Opening event, e.g. suppose you have something like:

    <ig:ContextMenuService.Manager>

                    <ig:ContextMenuManager>

                        <ig:ContextMenuManager.ContextMenu>

                            <ig:XamContextMenu Opening="XamContextMenu_Opening">

                                <ig:XamMenuItem Header="Copy"/>

                                <ig:XamMenuItem Header="Paste"/>

                            </ig:XamContextMenu>

                        </ig:ContextMenuManager.ContextMenu>

                    </ig:ContextMenuManager>

                </ig:ContextMenuService.Manager>

     

    You could do the following in the event handler:

     

     

    private void XamContextMenu_Opening(object sender, OpeningEventArgs e)

            {

                var items = e.GetClickedElements<XamTreeItem>();

     

                if (items.Count == 0)

                {

                    e.Cancel = true;

                    return;

                }

     

                items.First().IsSelected = true;

            }

    HTH,

     

Reply Children
No Data