如果我使用IValueConverter
,它可以工作,而IMultiValueConverter
返回相同的值,它就不能工作,为什么?
<Border Background="Red" Width="100" Height="100"
CornerRadius="{Binding Converter={vc:SingleAndMultiConverter}}" />
<Border Background="Red" Width="100" Height="100"
CornerRadius="{MultiBinding Converter={vc:SingleAndMultiConverter}}" />
public class SingleAndMultiConverter : MarkupExtension, IValueConverter, IMultiValueConverter
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Convert();
}
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Convert();
}
private object Convert()
{
return 15;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
多重绑定抛出以下错误:
BindingExpression生成的值对目标属性无效。;值="15"
Border.CornerRadius
属于CornerRadius
类型。值转换器应始终为属性返回正确的类型。
很难说它们为什么表现不同,大概是由于某种原因,在使用多绑定时,没有使用类型转换器的默认值转换。如果你深入研究源代码,你可能会发现一些东西,但这可能不是一段愉快的旅程。
H.B.说了什么+1
[ValueConversion(typeof(object[]),typeof(CornerRadius))]
public class Multi : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return new CornerRadius(Double.Parse("15"));
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}