Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining booleans using and, or, etc in WPF data binding

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.

like image 597
ACOMIT001 Avatar asked Oct 18 '25 23:10

ACOMIT001


1 Answers

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.

like image 111
stukselbax Avatar answered Oct 21 '25 13:10

stukselbax