为什么MultiBinding不适用于CornerRadius

时间:2012-03-03 16:49:49

标签: wpf multibinding cornerradius

如果我使用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'

2 个答案:

答案 0 :(得分:2)

Border.CornerRadius的类型为CornerRadius。值转换器应始终为属性返回正确的类型。

很难说出它们为什么表现不同,可能是因为某些无法解释的原因使用多重绑定时使用type converters没有默认值转换。如果您要深入了解源代码,您可能会找到一些东西,但这可能不会是一段愉快的旅程。

答案 1 :(得分:0)

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();
    }

}