开发者

How to determine which node was clicked. Silverlight treeview

开发者 https://www.devze.com 2023-01-07 20:21 出处:网络
How to determine on over which node click was performed? Treeview from silverlight toolkit. In MouseRightButtonUp i need to get node:

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

In MouseRightButtonUp i need to get node:

private void treeView_Mous开发者_如何学CeRightButtonUp(object sender, MouseButtonEventArgs e)


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)
                        .Ancestors()
                        .OfType<TreeViewItem>()
                        .FirstOrDefault();
0

精彩评论

暂无评论...
验证码 换一张
取 消