I have the following code in my WPF Application that I use to enable or disable buttons on my UI
XAML
<Button x:Name="FirstButton" Command="{x:Static local:MyClass.CommandOne}"/>
<Button x:Name="FirstButton" Command="{x:Static local:MyClass.CommandTwo}"/>
Code Behind
CommandBindings.Add(new CommandBinding(CommandOne, CommandOne_Executed, CommandOne_CanExecute));
CommandBindings.Add(new CommandBinding(CommandTwo, CommandTwo_Executed, CommandTwo_CanExecute));
public static readonly RoutedUICommand CommandOne = new RoutedUICommand();
public static readonly RoutedUICommand CommandTwo = new RoutedUICommand();
private void CommandOne_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (e != null)
e.CanExecute = (_currentValue > 1);
}
private void CommandTwo_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (e != null)
e.CanExecute = (_currentValue > 100);
}
Worker Code
private async Task DoSomeWork(int value)
{
await Task.Run(() =>
{
// Do some work on value
_currentValue = value
}
}
What I find is that when the values of _currentValue change due to some processing sometimes the CommandOne_CanExecute and CommandTwo_CanExecute functions do not get called. If I then e.g. move the UI they will then be called. How can I ensure that these are called everytime.