Ignacio Soler Garcia
Ignacio Soler Garcia

Reputation: 21855

How to bind the IsEnabled property to an OR of two values?

currently when I have to make an OR of two values on the IsEnabled property of a control I end using an invisible container control (I use a Border) and setting the IsEnabled of the control and the one of the container.

Is there a better approach? If not, what is the most lightweight control for doing this?

Thanks in advance.

Upvotes: 0

Views: 4472

Answers (4)

Hans Voralberg
Hans Voralberg

Reputation: 69

If you want to avoid MultiBinding, or if, for example you are on UWP or WinUI where MultiBinding is not supported, you could also use ContentControl as container control and set there the other IsEnabled binding.

Upvotes: 0

qqbenq
qqbenq

Reputation: 10460

You can use a converter like this:

public class BooleanOrConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        foreach (object value in values)
        {
            if ((value is bool) && (bool)value == true)
            {
                return true;
            }
        }
        return false;
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException("BooleanOrConverter is a OneWay converter.");
    }
}

And this is how you would use it:

<myConverters:BooleanOrConverter x:Key="BooleanOrConverter" />
...
<ComboBox Name="MyComboBox">
  <ComboBox.IsEnabled>
    <MultiBinding Converter="{StaticResource BooleanOrConverter}">
      <Binding ElementName="SomeCheckBox" Path="IsChecked" />
      <Binding ElementName="AnotherCheckbox" Path="IsChecked"  />
    </MultiBinding>
  </ComboBox.IsEnabled>
</ComboBox>

Upvotes: 3

Clemens
Clemens

Reputation: 128070

If IsEnabled is set via binding, you may use MultiBinding in conjunction with a multi-value converter.

Upvotes: 3

brunnerh
brunnerh

Reputation: 184752

Could use a MultiBinding with a converter which or's the values passed in.

Upvotes: 1

Related Questions