0

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.

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
  • 1
    If you're binding to a ViewModel, you can create an aggregate property and bind to that instead. – dlev Mar 14 '12 at 21:06
  • 1
    visit http://stackoverflow.com/questions/945427/c-sharp-wpf-isenabled-using-multiple-bindings – GANI Mar 14 '12 at 21:08

3 Answers3

3

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>
qqbenq
  • 10,220
  • 4
  • 40
  • 45
3

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

Clemens
  • 123,504
  • 12
  • 155
  • 268
1

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

H.B.
  • 166,899
  • 29
  • 327
  • 400