You can get the TreeViewItem by getting PlacementTarget of ContextMenu
which will be StackPanel
and its TemplatedParent
will be ContentPresenter
and its TemplatedParent
will be TreeViewItem
. So this will work:
CommandParameter="{Binding Path=PlacementTarget.TemplatedParent.TemplatedParent,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type ContextMenu}}}"
PlacementTarget (StackPanel) --> TemplatedParent (ContentPresenter) --> TemplatedParent (TreeViewItem)
Ideally it's not a good idea to pass UI components to ViewModel. You should pass data i.e. DataContext of TreeViewItem as you can always play with that.
In case you want to pass Server
instance i.e. DataContext
of TreeviewItem, you can simply do "{Binding}"
since MenuItem will inherit it from StackPanel.
CommandParameter="{Binding}"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…