MenuItem
doesn't lie in same Visual Tree as that of TreeViewItem
, that's why FindAncestor is not able to travel till Window.
You can use PlacementTarget
of ContextMenu
to get TreeViewItem which will inherit DataContext of window automatically.
This is how you do it:
<MenuItem Header="Test"
Command="{Binding PlacementTarget.DataContext.testCommand,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type ContextMenu}}}"/>