我有一个 Menu
,其中层次结构中的每个 MenuItem
都将其 Command
属性设置为我定义的 RoatedCommand
。关联的 CommandBinding 为 CanExecute 的评估提供回调,该回调控制每个 MenuItem
的启用状态。
这几乎有效。菜单项最初会显示正确的启用和禁用状态。但是,当我的 CanExecute
回调使用的数据发生更改时,我需要命令从回调中重新请求结果,以便在 UI 中反映此新状态。
RoatedCommand
或 CommandBinding
上似乎没有任何公共方法可以实现此目的。
请注意,当我单击或键入控件时,会再次使用回调(我猜它是在输入时触发的,因为鼠标悬停不会导致刷新)。
I have a Menu
where each MenuItem
in the hierarchy has its Command
property set to a RoutedCommand
I've defined. The associated CommandBinding
provides a callback for the evaluation of CanExecute
which controls the enabled state of each MenuItem
.
This almost works. The menu items initially come up with the correct enabled and disabled states. However when the data that my CanExecute
callback uses changes, I need the command to re-request a result from my callback in order for this new state to be reflected in the UI.
There do not appear to be any public methods on RoutedCommand
or CommandBinding
for this.
Note that the callback is used again when I click or type into the control (I guess it's triggered on input because mouse-over doesn't cause the refresh).
发布评论
评论(6)
不是书中最漂亮的,但您可以使用 CommandManager 使所有命令绑定无效:
请参阅 MSDN
Not the prettiest in the book, but you can use the CommandManager to invalidate all commandbinding:
See more info on MSDN
对于后来遇到这个问题的人;如果您碰巧使用 MVVM 和 Prism,那么 Prism 的
ICommand
的DelegateCommand
实现提供了一个.RaiseCanExecuteChanged()
方法来执行此操作。For anyone who comes across this later; If you happen to be using MVVM and Prism, then Prism's
DelegateCommand
implementation ofICommand
provides a.RaiseCanExecuteChanged()
method to do this.我无法使用 CommandManager.InvalidateRequerySuggested(); 因为我的性能受到了影响。
我使用了 MVVM Helper 的委托命令,如下所示(我根据我们的要求对其进行了一些调整)。你必须从VM调用
command.RaiseCanExecuteChanged()
I couldnt use
CommandManager.InvalidateRequerySuggested();
because I was getting performance hit.I have used MVVM Helper's Delegating command, which looks like below (i have tweaked it a bit for our req). you have to call
command.RaiseCanExecuteChanged()
from VM如果您已经推出了自己的实现
ICommand
的类,您可能会丢失大量自动状态更新,迫使您依赖于手动刷新而不是需要的。它还可能破坏InvalidateRequerySuggested()
。问题在于简单的ICommand
实现无法将新命令链接到CommandManager
。解决方案是使用以下内容:
这样订阅者就可以附加到 CommandManager 而不是您的类,并且可以正确参与命令状态更改。
If you have rolled your own class that implements
ICommand
you can lose a lot of the automatic status updates forcing you to rely on manual refreshing more than should be needed. It can also breakInvalidateRequerySuggested()
. The problem is that a simpleICommand
implementation fails to link the new command to theCommandManager
.The solution is to use the following:
This way subscribers attach to
CommandManager
rather than your class and can properly participate in command status changes.我已经实现了一个解决方案来处理命令的属性依赖性,此处链接 https://stackoverflow.com/a/30394333/1716620< /a>
感谢您最终会得到这样的命令:
I've implemented a solution to handle property dependency on commands, here the link https://stackoverflow.com/a/30394333/1716620
thanks to that you'll end up having a command like this:
这对我有用:将 CanExecute 放在 XAML 中的 Command 之前。
This is what worked for me: Put the CanExecute before the Command in the XAML.