How to bind the IsEnabled property to an OR of two values?
Question
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.
Solution
If IsEnabled
is set via binding, you may use MultiBinding in conjunction with a multi-value converter.
OTHER TIPS
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>
Could use a MultiBinding
with a converter which or's the values passed in.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow