上下文菜单命令绑定在树视图分层数据模板中不起作用



我有一个usercontrol和一个ViewModel。

我在 usercontrol.resources

内定义了上下文菜单

上下文菜单包含菜单项。我设置了Menuitem标头和命令属性。

deletecommand 在ViewModel中定义。我还使用reactrivationource 将benuitem的datacontext属性设置为usercontrol。

[以某种方式似乎无法正常工作]

在用户控件内部i具有树视图,其项目库是在ViewModel中定义的。TreeView具有parenthnode和Child Node。

我仅在儿童节点下显示上下文菜单,要实现此treeview使用两个 eRARCHICALDATATATEMPLATE levelTemplate1 and levelTemplate2 p>当我右键单击子节点时,我可以看到上下文菜单,但是当我单击 delete 时,deletecommand的Onexecute方法不会发射。

代码结构就像:

     <UserControl.Resources>
    <ContextMenu x:Key="childContextMenu" >
        <MenuItem DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext}"
                  Command="{Binding DeleteCommand}"
                  Header="Delete"/>
    </ContextMenu>
</UserControl.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.Resources>
        <HierarchicalDataTemplate x:Key="level2Template">
            <Grid ContextMenu="{StaticResource childContextMenu}">
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding}"/>
            </Grid>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="level1Template"
                                  ItemTemplate="{StaticResource level2Template}"
                                  ItemsSource="{Binding childs}"
                                  >
            <TextBlock Text="{Binding Name}"/>
        </HierarchicalDataTemplate>

        <Style TargetType="{x:Type TreeViewItem}" x:Key="TreeViewStyle">
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
            <Setter Property="FontWeight" Value="Normal" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="FontWeight" Value="Bold" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>
    <GroupBox Header="Tree" Grid.Column="0">
    <TreeView ItemTemplate="{StaticResource level1Template}"
              ItemsSource="{Binding Groups}"
              ItemContainerStyle="{StaticResource TreeViewStyle}">
    </TreeView>
    </GroupBox>
</Grid>

在输出窗口中显示错误

"找不到与参考的副产品绑定的来源 findancestor,祖先='system.windows.controls.usercontrol', 祖先='1''

我更改了 level2template 如下,它检测到 delete 命令binding

<HierarchicalDataTemplate x:Key="level2Template">
                <Grid Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=TreeView}}">
                    <Grid.ContextMenu>
                        <ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                            <MenuItem Header="Delete" Command="{Binding DeleteCommand}"/>
                        </ContextMenu>
                    </Grid.ContextMenu>
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition></ColumnDefinition>
                        <ColumnDefinition></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Text="{Binding}">
                    </TextBlock>
                </Grid>
            </HierarchicalDataTemplate>

最新更新