views:

17

answers:

1

How to determine on over which node click was performed? Treeview from silverlight toolkit.

In MouseRightButtonUp i need to get node:

private void treeView_MouseRightButtonUp(object sender, MouseButtonEventArgs e)

+1  A: 

The MouseButtonEventArgs has an OriginalSource property which indicates the actual UIElement that generated the event.

In order to discover which Node that element belongs to you will need to traverse the visual tree to discover it. I use this extension method to assist with that:-

    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 MouseRightButtonUp event you can use this code to find the item:-

 TreeViewItem node = ((DependencyObject)e.OriginalSource)
                        .OfType<TreeViewItem>()
                        .FirstOrDefault();
AnthonyWJones