2
我需要的一堆布爾屬性,但與反演有些像一個例子的multibinding:wpf如何使用轉換器進行多重綁定的子綁定?
<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.");
}
}
那麼,如何使用轉換器,孩子的綁定?
你知道你自己在拋出異常嗎?不要檢查'targetType'。檢查「value」是否爲bool,而不是返回。 – Clemens 2014-11-22 18:15:15