Binding an Ancestor not working WPF

前端 未结 1 1108
醉梦人生
醉梦人生 2021-01-20 14:33

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

相关标签:
1条回答
  • 2021-01-20 15:09

    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>
    
    0 讨论(0)
提交回复
热议问题