本文介绍了上下文菜单打开时可以执行ICommand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

控件具有其ContextMenu,并且其MenuItem绑定到ICommand. ICommand的Can Execute具有一些启用和禁用MenuItem的代码.这会在加载时命中并检查是否可以执行,但在打开ContexMenu时可能命中,因此在打开ContextMenu时每次都需要命中(检查).

A Control has its ContextMenu and its MenuItem is bound to an ICommand. ICommand's Can Execute has some code to enable and disable the MenuItem. This hits while loading and checks for Can Execute but it can hit while open the ContexMenu and it need to be hit(Checks) for each time while opening the ContextMenu.

<Border>
    <Border.ContextMenu> 
       <ContextMenu> 
           <MenuItem Command="{Binding SaveCommand, Source={StaticResource vm}}" CommandParameter="{Binding}" Header="Menu1" />
       </ContextMenu> 
    </Border.ContextMenu>
</Border>

如何做到这一点.有什么主意吗?

How could be done this. Any idea?

推荐答案

您应该从ViewModel触发事件ICommand.CanExecuteChanged(),这将导致控件从ViewModel调用CanExecute.

You should trigger the event ICommand.CanExecuteChanged() from ViewModel which shall cause the Control to invoke CanExecute from ViewModel.

这篇关于上下文菜单打开时可以执行ICommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 12:42