I like the answers that are provided already, but I thought I'd add my own version that I've been using sometimes. With the MVVM architecture, the View Model's properties are all calling SetProperty()
in the object setters, which raises the INotifyPropertyChanged
event. What I've been doing is adding a second property to the view model, and setting its value to !value
in the setter of the first property.
For example, I want a button to be button.IsEnabled = false
whenever the property ViewModel.IsBusy = true
is the case. This is a reverse bool example, just like you're asking about.
ViewModel.cs properties:
public bool EnableButton
{
get { return enableButton; }
set { SetProperty(ref enableButton, value); }
}
private bool isBusy;
bool IsBusy
{
get { return isBusy; }
set
{
SetProperty(ref isBusy, value);
EnableButton = !value;
}
}
XAML:
<ActivityIndicator IsRunning="{Binding IsBusy}"/>
<Button Text="Start Sync"
Command="{Binding SyncCommand}"
IsEnabled="{Binding EnableButton}"/>
This way, anytime in my code that I call IsBusy = true
, it will simultaneously set button.IsEnabled = false
, solving the reverse bool problem.