Does anyone know how I can force CanExecute
to get called on a custom command (Josh Smith's RelayCommand
)?
Typically, CanExecute
is called whenever interaction occurs on the UI. If I click something, my commands are updated.
I have a situation where the condition for CanExecute
is getting turned on/off by a timer behind the scenes. Because this is not driven by user interaction, CanExecute
is not called until the user interacts with the UI. The end result is that my Button
remains enabled/disabled until the user clicks on it. After the click, it is updated correctly. Sometimes the Button
appears enabled, but when the user clicks it changes to disabled instead of firing.
How can I force an update in code when the timer changes the property that affects CanExecute
? I tried firing PropertyChanged
(INotifyPropertyChanged
) on the property that affects CanExecute
, but that did not help.
Example XAML:
<Button Content="Button" Command="{Binding Cmd}"/>
Example code behind:
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; }
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…