RSS

Generic IValueConverter

05 Oct

During WPF-Application developement I thought that it might be useful to have a generic IValueConverter to have the possibility to check some errors at runtime already. Therefore I implemented following generic ValueConverter:

public abstract class ValueConverter <TSource, TTarget, TParameter> : IValueConverter
{
    protected abstract TTarget Convert(TSource value,
                                   Type targetType,
                                   TParameter parameter,
                                   CultureInfo culture);
    protected abstract TSource ConvertBack(TTarget value,
                                   Type targetType,
                                   TParameter parameter,
                                   CultureInfo culture);

    object IValueConverter.Convert(object value,
                                   Type targetType,
                                   object parameter,
                                   CultureInfo culture)
    {
        if (value != null && !(value is TSource))
            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(TSource)));
        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((TSource)value, targetType, (TParameter)parameter, culture);
    }

    object IValueConverter.ConvertBack(object value,
                                       Type targetType,
                                       object parameter,
                                       CultureInfo culture)
    {
        if (value != null && !(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, targetType, (TParameter)parameter, culture);
    }
}

See the second post of this series regarding generic IMultiValueConverter

The tree Generic parameters defines the type that is passed, returned and the type of the additional ConverterParameter.

In order to make it a lot easier to use you can reduce the number of generic parameters implementing following classes:

public abstract class ValueConverter<TSourceTarget> : ValueConverter<TSourceTarget, TSourceTarget> {}
public abstract class ValueConverter<TSource, TTarget> : ValueConverter<TSource, TTarget, object> {}

A very simple class to show you the functionality would be a InverseBooleanConverter:

public class InverseBooleanConverter<bool> : ValueConverter
{
    protected override bool Convert(bool value, Type targetType, object parameter, CultureInfo culture)
    {
        return !value;
    }

    protected override bool ConvertBack(bool value, Type targetType, object parameter, CultureInfo culture)
    {
        return !value;
    }
}

or a BooleanToVisibilityConverter

public class BoolToVisibilityConverter<bool,Visibility> : ValueConverter
{
    protected override Visibility Convert(bool value, Type targetType, object parameter, CultureInfo culture)
    {
        return value ? Visibility.Visible : Visibility.Collapsed;
    }

    protected override bool ConvertBack(Visibility value, Type targetType, object parameter, CultureInfo culture)
    {
        return value == Visibility.Visible;
    }
}

Continue reading on the second post of this series regarding generic IMultiValueConverter

Advertisements
 
Leave a comment

Posted by on October 5, 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: