WPF 中 ContextMenu 中的 CommandParameters [英] CommandParameters in ContextMenu in WPF

查看:28
本文介绍了WPF 中 ContextMenu 中的 CommandParameters的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我有一个 WPF TreeView 控件,它的项目有一个 HierarchicalDataTemplate.现在在 HierarchicalDataTemplate 中,我有一个 Label 并且 Label 有一个 ContextMenu 和一个用于 的菜单项删除.删除菜单项绑定到名为 DeleteCommand 的命令,该命令是已设置为 HierarchicalDataTemplateDataType 的类的一部分.

I have a scenario where I have a WPF TreeView control that has an HierarchicalDataTemplate for its items. Now inside the HierarchicalDataTemplate, I have a Label and the Label has a ContextMenu with a menuitem for Delete. The Delete menuitem is bound to a Command called DeleteCommand which is a part of the class that has been set as the DataType of the HierarchicalDataTemplate.

现在,我想在ContextMenu的Delete菜单项的DeleteCommandCommandParameters中传递TreeView控件,所以我可以在删除当前选定的项目时处理 TreeViewItems 的选择.

Now, I want to pass the TreeView control in the CommandParameters of the ContextMenu's Delete menuitem's DeleteCommand so that I can handle the selection of the TreeViewItems on the deletion of the currently selected item.

但是,如果我将 CommandParameters 绑定为 {Binding ElementName=TreeViewName} 或其他任何相关的内容,则它始终为 null,除非绑定元素是DataContext.

But if I bind the CommandParameters as the {Binding ElementName=TreeViewName} or whatever for that matter, it is always null unless the binded element is a property in the DataContext.

任何人都可以帮助我解决方案,因为我认为,我已经尝试了所有可能的事情,例如 RelativeSource 和 AncestorType 等,但它始终为空.对我来说,它看起来像是框架中的限制或错误.

Can anyone help me with a solution because I think, I have tried all the possible things such as RelativeSource and AncestorType etc but its always null. To me, it looks like either a limitation or a bug in the framework.

推荐答案

问题在于 ContextMenu 位于其自己的可视化树的根部,因此任何 RelativeSource.FindAncestor 绑定都不会越过 ContextMenu.

The problem is that the ContextMenu is at the root of its own visual tree, so any RelativeSource.FindAncestor bindings won't go past the ContextMenu.

一种解决方案是使用 PlacementTarget 属性从您的标签设置两阶段绑定:

One solution is to use the PlacementTarget property to set up a two-stage binding from your Label:

<Label Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={
    x:Type TreeView}}}">
    <Label.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Delete" Command="{x:Static local:Commands.DeleteCommand}"
                CommandParameter="{Binding PlacementTarget.Tag, RelativeSource={
                RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>
        </ContextMenu>
    </Label.ContextMenu>
</Label>

然而,这很hacky.您最好设置 CommandTarget 将 MenuItem 的属性添加到 ContextMenu 的 PlacementTarget 并在 TreeView 上具有命令处理程序.这意味着您不必传递 TreeView.

This is quite hacky, however. You're better off setting the CommandTarget property of your MenuItem to the ContextMenu's PlacementTarget and having the command handler on your TreeView. This means you won't have to pass the TreeView around.

这篇关于WPF 中 ContextMenu 中的 CommandParameters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆