【问题标题】:RelayCommand CanExecute behaviorRelayCommand CanExecute 行为
【发布时间】:2013-02-10 12:47:01
【问题描述】:

我有以下命令:

<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}"/>

StartProductionCommand = new RelayCommand(OpenConnection, CanStartProduction);

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

CanStartProduction 仅在我重新调整 UI 大小而不是即时更新时才会检查。 知道为什么每次更改值时都不更新吗?

【问题讨论】:

    标签: c# wpf mvvm mvvm-light


    【解决方案1】:

    CommandManager 无法知道该命令依赖于LogContent_simulationObject.Connected,因此当这些属性发生变化时,它无法自动重新评估CanExecute

    您可以通过调用CommandManager.InvalidateRequerySuggested 明确请求重新评估。请注意,它将为 all 命令重新评估 CanExecute;如果您只想刷新一个,则需要通过调用StartProductionCommand.RaiseCanExecuteChanged 在命令本身上引发CanExecuteChanged 事件。

    【讨论】:

    • 你能举个例子或发布链​​接如何使用它吗?我的意思是你怎么称呼它?
    • @batmaci,当您希望重新评估命令的 CanExecute 时,只需调用 CommandManager.InvalidateRequerySuggested
    【解决方案2】:

    您可以在 For example PropertyChanged Eventhandler 中调用 RaiseCanExecuteChanged。

    命令状态不会经常刷新。

    前段时间我读了一篇关于它的好文章。我稍后会发布。

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

    另见Refresh WPF Command

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2011-01-19
      • 2014-09-29
      • 1970-01-01
      相关资源
      最近更新 更多