当其所有子项都被禁用时,如何禁用顶级 WPF MenuItem?
我的 WPF 应用程序中的上下文菜单中有以下 MenuItem:
<MenuItem Header="Email">
<MenuItem Command="Commands:CommandRepository.GenerateUserEmailCommand"
CommandParameter="{Binding Path=SelectedItems}"
Header="Email User">
</MenuItem>
<MenuItem Command="Commands:CommandRepository.GenerateManagerEmailCommand"
CommandParameter="{Binding Path=SelectedItems}"
Header="Email Manager">
</MenuItem>
</MenuItem>
问题是,当两个电子邮件命令都返回 CanExecute = false 时,因此这两个命令都被禁用,顶级 MenuItem“电子邮件”仍保持启用状态。我知道我可能可以将顶部菜单项的 IsEnabled 绑定到其 Children 属性,然后使用转换器来决定何时应禁用它,但似乎这应该自动发生。这不是使用 CommandBindings 的全部意义吗(即它们为您处理 IsEnabled)?有更好的方法来实现这一点吗?
I have the following MenuItem inside of a Context Menu in my WPF application:
<MenuItem Header="Email">
<MenuItem Command="Commands:CommandRepository.GenerateUserEmailCommand"
CommandParameter="{Binding Path=SelectedItems}"
Header="Email User">
</MenuItem>
<MenuItem Command="Commands:CommandRepository.GenerateManagerEmailCommand"
CommandParameter="{Binding Path=SelectedItems}"
Header="Email Manager">
</MenuItem>
</MenuItem>
The issue is that when both of the Email commands return CanExecute = false, and therefore both commands get disabled, the top-level MenuItem "Email" remains enabled. I know I could probably bind the IsEnabled of the top menu item to its Children property and then use a converter to decide when it should be disabled, but it seems like this should be happening automatically. Isn't this the whole point of using CommandBindings (i.e. they take care of IsEnabled for you)? Any better way to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以想到两种方法来做到这一点:
1)如果您不介意后面的代码(我尝试使用 WPF 避免这种情况),您可以为 IsEnabledChanged 创建一个事件处理程序来检查 IsEnabled 属性是否设置为 false。如果是的话,您可以执行类似
2) 为父菜单项和子菜单项类型创建 ViewModel 并将父菜单项视图的 is Enabled 绑定到视图模型的 IsEnabled 属性的操作。视图模型将使用类似的方法返回 false
I can think of two ways to do this:
1) If you don't mind code behind (I try to avoid it with WPF) you could just create an event handler for IsEnabledChanged that checks if the IsEnabled Property was set to false. If it was you could then do something like
2) Create a ViewModel for the Parent MenuItem and Child MenuItem types and bind the is Enabled of the Parent MenuItem View to a IsEnabled property of the view model. The view model would return false using a similar
为什么你的“电子邮件”菜单项应该被禁用?仅仅因为孩子们是?
我认为您使用多重绑定和转换器的方法是实现您想要的功能的好方法。
why should your "Email" MenuItem become disabled? just because the childrens are?
i think your approach to use multibinding and a converter is a good way to do what YOU want.
定义一个 RootMenu 命令,然后将其添加到根菜单
将命令绑定到下面的 RootMenuCanExecute
它有点消耗 cpu 资源,但它可以工作。
不要有太多的 MenuItem 子项
Define a RootMenu command then add it to the root menu
Bind the command to the following RootMenuCanExecute
it is somewhat cpu expensive but it works.
Whatch out not to have too many MenuItem children