I'd like to know if anyone has any suggestions for combining multiple Boolean values using AND, OR, etc in WPF data bindings. Ideally this should be MVVM compliant.
For example:
<TextBox Style="{StaticResource DataEntryField-Medium}"
Text="{Binding FirstName}"
IsEnabled="{Binding IsCustomerSelected AND IsEditingEnabled}" />
I'd like to have a text box that is only enabled if a customer is selected and editing is enabled.
You also have an option to create composite property in your ViewModel, like this:
public bool CanEditCustomer
{
get { return IsCustomerSelected && IsEditingEnabled; }
}
and inside the setters of that properties you should raise PropertyChanged event for that composite property:
public bool IsCustomerSelected
{
....
set
{
if (value != _isCustomerSelected)
{
_isCustomerSelected = value;
RaisePropertyChaged("IsCustomerSelected");
RaisePropertyChaged("CanEditCustomer");
}
}
}
and finally xaml:
<TextBox Style="{StaticResource DataEntryField-Medium}"
Text="{Binding FirstName}"
IsEnabled="{Binding CanEditCustomer}" />
This way you do not need a converter, but you have to care about raising PropertyChanged notification in your ViewModel when it is required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With