有谁知道我怎么可以强制CanExecute调用自定义命令(约什史密斯RelayCommand)?
通常,CanExecute只要在UI上发生交互,就会调用它.如果我点击某些内容,我的命令会更新.
我有一种情况,CanExecute即幕后的计时器打开/关闭条件.因为这不是由用户交互驱动的,CanExecute所以在用户与UI交互之前不会调用.最终结果是我的Button保持启用/禁用,直到用户点击它.点击后,它会正确更新.有时Button显示已启用,但是当用户单击它时,更改为禁用而不是触发.
当计时器更改影响的属性时,如何强制更新代码CanExecute?我对影响的属性尝试了kick PropertyChanged(INotifyPropertyChanged)CanExecute,但这没有帮助.
示例XAML:
<Button Content="Button" Command="{Binding Cmd}"/>
Run Code Online (Sandbox Code Playgroud)
后面的示例代码:
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; }
Run Code Online (Sandbox Code Playgroud)