1

似乎无法将我的上下文菜单中的菜单项绑定到我的视图模型中的命令。我知道上下文菜单不在可视树中。出于测试目的,我在一个按钮中绑定了两次相同的命令。第一个绑定有效,但我可以在上下文菜单中获得第二个绑定来绑定。我可以在输出中看到绑定错误。有人有什么想法吗?

<HierarchicalDataTemplate DataType="{x:Type inf:OSiteEquipment}" ItemsSource="{Binding Path=SubSystems, Converter={StaticResource subsystemConverter}}"  >
                <Button HorizontalContentAlignment="Left"
                        Command="{Binding DataContext.CommandOpenSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
                        CommandParameter="{Binding}">

                    <TextBlock Text="{Binding Path=PartData.Name}" TextTrimming="CharacterEllipsis" />
                    <Button.ContextMenu>
                        <ContextMenu>                                                                         

     <MenuItem Header="Add Category"  Command="{Binding Path=Parent.PlacementTarget.Tag.CommandOpenSelected, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
                        </ContextMenu>

                    </Button.ContextMenu>
                </Button>
            </HierarchicalDataTemplate>
4

2 回答 2

0

只需删除数据绑定路径中的“父级”:

<HierarchicalDataTemplate DataType="{x:Type inf:OSiteEquipment}" ItemsSource="{Binding Path=SubSystems, Converter={StaticResource subsystemConverter}}"  >
    <Button HorizontalContentAlignment="Left"
            Command="{Binding DataContext.CommandOpenSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
            CommandParameter="{Binding}">

        <TextBlock Text="{Binding Path=PartData.Name}" TextTrimming="CharacterEllipsis" />
        <Button.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Add Category"  Command="{Binding Path=PlacementTarget.Command, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
            </ContextMenu>
        </Button.ContextMenu>
    </Button>
</HierarchicalDataTemplate>
于 2012-08-29T08:08:59.490 回答
0
  1. 您已经通过相对源找到了->在路径中Parent省略了Parent
  2. 您甚至没有设置Tag( PlacementTarget) Button-> 分别设置它
于 2012-08-29T09:39:32.737 回答