RSS

Generic IMultiValueConverter

06 Oct

In addition to the first part of this post series I want to present the generic pendant for IMultiValueConverter:

public abstract class MultiValueConverter<TTarget, TParameter> : IMultiValueConverter
{
    protected abstract TTarget Convert(object[] values,
                                       Type targetType,
                                       TParameter parameter,
                                       CultureInfo culture);
    protected abstract object[] ConvertBack(TTarget value,
                                       Type[] targetTypes,
                                       TParameter parameter,
                                       CultureInfo culture);

    object IMultiValueConverter.Convert(object[] values,
                                        Type targetType,
                                        object parameter,
                                        CultureInfo culture)
    {
        if (parameter != null && !(parameter is TParameter))
            throw new InvalidCastException(string.Format("In order to use the generic IValueConverter you have to use the correct type as ConvertParameter. The passing type was {0} but the expected is {1}", parameter.GetType(), typeof(TParameter)));

        return Convert(values, targetType, (TParameter)parameter, culture);
   }

    object[] IMultiValueConverter.ConvertBack(object value,
                                        Type[] targetTypes,
                                        object parameter,
                                        CultureInfo culture)
    {
        if (!(value is TTarget))
            throw new InvalidCastException(string.Format("In order to use the generic IValueConverter you have to use the correct type.
                                        The passing type was {0} but the expected is {1}", value.GetType(), typeof(TTarget)));
        if (parameter != null && !(parameter is TParameter))
           throw new InvalidCastException(string.Format("In order to use the generic IValueConverter you have to use the correct type as ConvertParameter. The passing type was {0} but the expected is {1}", parameter.GetType(), typeof(TParameter)));

        return ConvertBack((TTarget)value, targetTypes, (TParameter)parameter, culture);
    }
}

The usage is the same as you can see in the previous post. Have a look.
Like in the previous section following snippet will make it easies to use if you do not need all generic types

public abstract class MultiValueConverter<TTarget> : MultiValueConverter<TTarget, object> {}
Advertisements
 
Leave a comment

Posted by on October 6, 2011 in C-Sharp, WPF

 

Tags:

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: