I\'ve got a TreeView, which is modified to show Images in front of the text too. So my modified TreeViewItem is called ImagedTreeViewItem. This ImagedTreeViewItem has a Prop
ContextMenu
is not part of the VisualTree, that's why the binding fails. You have to use some kind of relay: ContextMenu.PlacementTarget
and Tag
property as a cache for the second trail of binding search. I think this will work:
<StackPanel Orientation="Horizontal"
Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type myClasses:ImagedTreeViewItem}}}">
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Command="my:ImagedTreeView.AddFolder" Header="Add Folder"
IsEnabled="{Binding Path=PlacementTarget.Tag.IsFolder, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
<MenuItem.Icon>
<Image Source="folderadd16.png" />
</MenuItem.Icon>
</MenuItem>
<!-- ... -->
</ContextMenu>
</StackPanel.ContextMenu>