首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RelayCommand CanExecute行为

RelayCommand CanExecute行为
EN

Stack Overflow用户
提问于 2013-02-25 19:25:56
回答 2查看 11.5K关注 0票数 7

我有以下命令:

代码语言:javascript
复制
<Button x:Name="bOpenConnection" Content="Start Production"
        Grid.Row="0" Grid.Column="0"
        Height="30" Width="120" Margin="10"
        HorizontalAlignment="Left" VerticalAlignment="Top" 
        Command="{Binding Path=StartProductionCommand}"/>

代码语言:javascript
复制
StartProductionCommand = new RelayCommand(OpenConnection, CanStartProduction);

private bool CanStartProduction()
{
   return LogContent != null && !_simulationObject.Connected;
}

只有当我调整UI大小并且没有动态更新时,CanStartProduction才会被选中。你知道为什么他们每次改变值时都不更新吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-25 19:29:29

CommandManager无法知道命令依赖于LogContent_simulationObject.Connected,因此当这些属性更改时,它不能自动重新计算CanExecute

您可以通过调用CommandManager.InvalidateRequerySuggested显式地请求重新评估。请注意,它将重新计算所有命令的CanExecute;如果只想刷新一个命令,则需要通过调用StartProductionCommand.RaiseCanExecuteChanged在命令本身上引发CanExecuteChanged事件。

票数 16
EN

Stack Overflow用户

发布于 2013-02-25 19:29:05

例如,您可以在PropertyChanged Eventhandler中调用RaiseCanExecuteChanged。

命令状态不会经常刷新。

不久前,我读到了一篇关于它的好文章。我稍后会把它贴出来。

另请参阅http://joshsmithonwpf.wordpress.com/2008/06/17/allowing-commandmanager-to-query-your-icommand-objects/

另请参阅Refresh WPF Command

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15065885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档