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();