Search code examples
wpfmultibindingcornerradius

Why does the MultiBinding not work for CornerRadius


If i use an IValueConverter it works, while with an IMultiValueConverter returning the same value it does not, why is that?

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

The multi-binding throws this error:

Value produced by BindingExpression is not valid for target property.; Value='15'


Solution

  • What H.B. said +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();
        }
    
    }