Base Class for ValueConverters in WPF
29-12-2015 15:26:08
C# / WPF
0 Bookmark(s)
264 View(s)
The trick is to inherit any of your IValueConverter implementations from MarkupExtension and return this. So you can use them directly inline, instead of adding them to the resources and referencing by name. Second advantage of this approach is that you can use as many typed properties on your converter and don't have to mess with the generic ConverterParameter.
public abstract class ValueConverterBase : MarkupExtension, IValueConverter
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
Example usage: