【问题标题】:RoutedCommand defaults to cannot executeRoutedCommand 默认无法执行
【发布时间】:2011-10-01 22:48:54
【问题描述】:

我在上下文菜单中使用自定义 RoutedCommand。命令定义如下:

public static readonly RoutedCommand RoutedAddMainHeading = new RoutedCommand();

它是这样访问的:

<ContextMenu>
    <MenuItem Header="Add Main Heading" Command="local:JurisdictionVM.RoutedAddMainHeading" />
</ContextMenu>

它工作正常,除了当我的程序启动时,该命令被禁用。在我点击窗口中的文本框后,它就会启用。

如果我使用自定义 ICommand 实现(CanExecute 始终返回 true),则不会发生这种情况,但不会路由该命令。

我尝试调用 CommandManager.InvalidateRequerySuggested,但这似乎没有任何作用。

我正在使用命令接收器来定义命令绑定并将其链接到我的 ViewModel,这可能会导致问题。我正在使用在这里找到的实现:http://www.codeproject.com/KB/WPF/VMCommanding.aspx

更新: 自定义命令绑定似乎不是问题所在。我尝试了一个链接到代码隐藏方法的标准 CommandBinding 并且遇到了同样的问题。 CommandBinding 的定义如下:

<CommandBinding Command="local:JurisdictionVM.RoutedAddMainHeading" CanExecute="Foo_CanExecute" Executed="Foo_Executed"/>

这似乎是 ManuItem 独有的问题。如果我在按钮中使用此命令,则不会出现此问题。

还有其他方法可以强制重新评估 CanExecute 吗?

或者一种让 RoutedCommand 默认启用的方法(或一种编写自定义命令的方法)?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      看起来和这里的问题一样:

      Why is this WPF RoutedCommand bound Context MenuItem disabled?

      解决方法是在窗口构造函数中设置焦点。

      【讨论】:

        猜你喜欢
        • 2015-08-09
        • 1970-01-01
        • 2015-04-02
        • 2012-06-06
        • 2011-03-12
        • 2016-10-05
        • 2016-09-22
        • 2015-03-22
        • 2014-11-17
        相关资源
        最近更新 更多