我需要一个多绑定的布尔属性集合,但是需要对其中一些进行反转,就像这个例子一样:
那么,如何在子绑定中使用转换器呢?
<StackPanel>
<StackPanel.IsEnabled>
<MultiBinding Converter="{StaticResource BooleanAndConverter}">
<Binding Path="IsInitialized"/>
<Binding Path="IsFailed" Converter="{StaticResource InverseBooleanConverter}"/>
</MultiBinding>
</StackPanel.IsEnabled>
</StackPanel.IsEnabled>
但我从一个名为 InverseBooleanConverter
的程序中得到了一个 InvalidOperationException
错误,错误信息是“目标必须是布尔值”。我的 InverseBooleanConverter
代码如下:
[ValueConversion(typeof(bool), typeof(bool))]
public class InverseBooleanConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (targetType != typeof(bool))
throw new InvalidOperationException("The target must be a boolean");
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
}
而BooleanAndConverter是:
public class BooleanAndConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return values.All(value => (!(value is bool)) || (bool) value);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException("BooleanAndConverter is a OneWay converter.");
}
}
那么,如何在子绑定中使用转换器呢?