首页 文章

ContextMenu命令绑定在treeview hierarchicaldatatemplate中不起作用

提问于
浏览
-2

我有一个usercontrol和一个viewmodel .

我在 UserControl.Resources 中定义了上下文菜单

上下文菜单包含一个菜单项 . 我设置了MenuItem头和命令属性 .

DeleteCommand 在viewmodel中定义 . I have also set datacontext property of MenuItem to usercontrol using RelativeSource .

[somehow this DataContext binding doesn't seems to be working]

在用户控件内部我有treeview,它的ItemSource在viewmodel中定义 . TreeView具有ParentNode和子节点 .

我只在子节点下显示上下文菜单并实现此Treeview使用两个 hierarchicaldatatemplate levelTemplate1levelTemplate2

当我右键单击子节点时,我可以看到上下文菜单但是当我点击 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>

在输出窗口中显示错误

“无法找到与引用绑定的源'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.UserControl',AncestorLevel ='1''

1 回答

  • 0

    我更改了 level2Template ,如下所示,它检测到 Delete 命令绑定

    <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>
    

相关问题