c# - RelayCommand CanExecute 行为

标签 c# wpf mvvm mvvm-light

我有以下命令:

<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 大小时选中,而不是动态更新。 知道为什么每次更改值时它都没有更新吗?

最佳答案

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

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

关于c# - RelayCommand CanExecute 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15065885/

相关文章:

c# - GUI 处理数据的速度不够快,大约 10 秒内无法使用

c# - 使用 MVVM Light 时如何将弹出窗口绑定(bind)到 ViewModel - BindingExpression 路径错误

c# - 使用MVVM,如何使用App.config动态生成 View

c# - WPF MVVM - 如何检测 View 是否为 "Dirty"

wpf - 中继命令未在菜单项单击时触发

wpf - 我应该在 MVVM 中使用 DTO 作为我的数据模型吗?

c# - .NET 中带有滚动条的下拉菜单

c# - C# smtp.gmail.com 和端口号 587 中目标主机拒绝发送电子邮件

c# - 在鼠标悬停时重新定位图像

wpf - F# WPF 设置自定义程序集标题