How to determine which node was clicked. Silverlight treeview
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();
精彩评论