I have something like this in my ViewModel:
public enum WorkState
{
Idle, Working, Stopping
};
public class MainViewModel : ViewModelBase
{
public const string StatePropertyName = "State";
private WorkState _state = WorkState.Idle;
public WorkState State
{
get
{ return _state; }
set
{
if (_state == value)
{
return;
}
RaisePropertyChanging(StatePropertyName);
_state = value;
RaisePropertyChanged(StatePropertyName);
StartStopCommand.RaiseCanExecuteChanged(); // <—————————————
}
}
private RelayCommand _startStopCommand;
public RelayCommand StartStopCommand
{
get
{
return _startStopCommand
?? (_startStopCommand = new RelayCommand(
() =>
{
if (State == WorkState.Idle)
{
State = WorkState.Working;
}
else if (State == WorkState.Working)
{
State = WorkState.Stopping;
new Thread(() =>
{
Thread.Sleep(5000);
State = WorkState.Idle;
}).Start();
}
},
() => (State == WorkState.Idle ||
(State == WorkState.Working)));
}
}
}
And button in my View:
<Button Command="{Binding StartStopCommand}">
<Button.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding State}" Value="{x:Static vm:WorkState.Idle}">
<Setter Property="Button.Content" Value="Start"/>
</DataTrigger>
<DataTrigger Binding="{Binding State}" Value="{x:Static vm:WorkState.Working}">
<Setter Property="Button.Content" Value="Stop"/>
</DataTrigger>
<DataTrigger Binding="{Binding State}" Value="{x:Static vm:WorkState.Stopping}">
<Setter Property="Button.Content" Value="Stop"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
But when I press Stop button and the State property changes to Idle, button only changes its' text to "Start" but remains disabled until I click somewhere in the window.
I added RaiseCanExecuteChanged to the setter of the State property but it didn't help.
What am I doing wrong?
Also I am not sure that this approach with Start/Stop in the same command and DataTriggers for settings button text is the best so I would be grateful if someone shares better way to do this.