ContextMenu中的CommandBinding

时间:2013-03-27 02:52:48

标签: c# wpf contextmenu hierarchicaldatatemplate commandbinding

我有一个TreeView并创建了一个基本的TreeItem类型。每个TreeItem都有一个标题,一个用于子项的TreeItem Collection和一个可能的上下文菜单的集合。 TreeItem类具有以下对象:

public delegate void dExecute(TreeItem item);

public dExecute ExecuteTarget { get; set; }
public object Tag { get; set; }
public string Header { get; set; }
public List<TreeItem> Children { get; set; }
public List<TreeItem> ContextMenu { get; set; }

上下文菜单再次使用HierarchicalDataTemplate显示TreeItem对象(我在树视图和上下文菜单中使用TreeItem类)。上下文菜单如下所示:

<ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" Visibility="{Binding ShowContextMenu}" ItemsSource="{Binding ContextMenu}">
    <ContextMenu.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Header}"  />
            <HierarchicalDataTemplate.ItemContainerStyle>
                <Style TargetType="MenuItem">
                    <Setter Property="Command" Value="{Binding Execute}"/>
                </Style>
            </HierarchicalDataTemplate.ItemContainerStyle>
        </HierarchicalDataTemplate>
    </ContextMenu.ItemTemplate>
</ContextMenu>

上下文菜单按我希望的方式呈现。我创建了一个上下文菜单,我只是附加到树视图中的一些项目。这是它的内容。

public List<TreeItem> ContextMenu
{
    get
    {
        List<TreeItem> list = new List<TreeItem>();
        TreeItem ti = new TreeItem("Some Action") { ExecuteTarget = targetMethod};
        list.Add(ti);
        ti = new TreeItem("test");
        ti.Children.Add(new TreeItem("foo") { ExecuteTarget = targetMethod});
        ti.Children.Add(new TreeItem("bar") { ExecuteTarget = targetMethod});
        ti.Children.Add(new TreeItem("foo") { ExecuteTarget = targetMethod});
        TreeItem ti2 = new TreeItem("inner"){ ExecuteTarget = targetMethod};
        ti.Children.Add(ti2);
        ti2.Children.Add(new TreeItem("foo") { ExecuteTarget = targetMethod});
        list.Add(ti);
        return list;
    }
}

上下文菜单如下所示。

The rendered context menu

看起来应该如此。这些命令可以正常工作。除了上下文菜单的最高级别的命令。当我点击“Some Action”时没有任何反应。我假设我必须向XAML添加一些东西,但我不知道在哪里。

1 个答案:

答案 0 :(得分:1)

<ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" Visibility="{Binding ShowContextMenu}" ItemsSource="{Binding ContextMenu}">
    <ContextMenu.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Header}"  />
            <HierarchicalDataTemplate.ItemContainerStyle>
                <Style TargetType="MenuItem">
                    <Setter Property="Command" Value="{Binding Execute}"/>
                </Style>
            </HierarchicalDataTemplate.ItemContainerStyle>
        </HierarchicalDataTemplate>
    </ContextMenu.ItemTemplate>

    <!-- this is what you're missing -->
    <ContextMenu.ItemContainerStyle>
        <Style TargetType="MenuItem">
            <Setter Property="Command" Value="{Binding Execute}"/>
        </Style>
    </ContextMenu.ItemContainerStyle>
</ContextMenu>