有谁知道我如何强制CanExecute调用自定义命令(Josh Smith's RelayCommand)?
通常,CanExecute在 UI 上发生交互时调用。如果我单击某些东西,我的命令就会更新。
我有一种情况,条件CanExecute是由幕后的计时器打开/关闭。因为这不是由用户交互驱动的,CanExecute所以在用户与 UI 交互之前不会调用。最终结果是我的Button保持启用/禁用状态,直到用户点击它。单击后,它已正确更新。有时Button显示已启用,但当用户单击时,它会变为禁用而不是触发。
当计时器更改影响的属性时,如何强制更新代码CanExecute?我尝试在影响的属性上触发PropertyChanged( ) ,但这没有帮助。INotifyPropertyChangedCanExecute
示例 XAML:
<Button Content="Button" Command="{Binding Cmd}"/>
后面的示例代码:
private ICommand m_cmd;
public ICommand Cmd
{
if (m_cmd == null)
m_cmd = new RelayCommand(
(param) => Process(),
(param) => EnableButton);
return m_cmd;
}
// Gets updated from a timer (not direct user interaction)
public bool EnableButton { get; set; }