How to determine which node was clicked. Silverview treeview

How to determine by which click the node was made? Treeview from silverlight toolkit.

In MouseRightButtonUp, I need to get node:

private void treeView_MouseRightButtonUp (object sender, MouseButtonEventArgs e)

+3
source share
1 answer

MouseButtonEventArgshas a property OriginalSourcethat indicates the actual UIElementthat generated the event.

To find out to which Node this element belongs to you, you will need to cross the visual tree to find it. I use this extension method to help with this: -

    public static IEnumerable<DependencyObject> Ancestors(this DependencyObject root)
    {
        DependencyObject current = VisualTreeHelper.GetParent(root);
        while (current != null)
        {
            yield return current;
            current = VisualTreeHelper.GetParent(current);
        }
    }

Then in the event MouseRightButtonUpyou can use this code to search for an element: -

 TreeViewItem node = ((DependencyObject)e.OriginalSource)
                        .Ancestors()
                        .OfType<TreeViewItem>()
                        .FirstOrDefault();
+3

Source: https://habr.com/ru/post/1755465/


All Articles