有谁知道我如何强制CanExecute
调用自定义命令(Josh Smith's RelayCommand
)?
通常,CanExecute
在 UI 上发生交互时调用。如果我单击某些东西,我的命令就会更新。
我有一种情况,条件CanExecute
是由幕后的计时器打开/关闭。因为这不是由用户交互驱动的,CanExecute
所以在用户与 UI 交互之前不会调用。最终结果是我的Button
保持启用/禁用状态,直到用户点击它。单击后,它已正确更新。有时Button
显示已启用,但当用户单击时,它会变为禁用而不是触发。
当计时器更改影响的属性时,如何强制更新代码CanExecute
?我尝试在影响的属性上触发PropertyChanged
( ) ,但这没有帮助。INotifyPropertyChanged
CanExecute
示例 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; }