WPF: Is there a way to use a ValueConverter without defining a resource? -


Is there a way to use ValueConverter without defining it in a resource? Syntax is very beautiful.

You can use the markup extension to reduce the amount of xaml code. Example: Public class MyConverter: Markup Extension, IValueConverter

{Private Static MyConverter _converter; Public object convert (type object type, type target type, object parameter, System.Globalization.CultureInfo culture) {// Convert and return some} Public Object Convertback (object value, type target Type, object parameter, system. Globalization.CultureInfo culture {// Convert and return Some (if needed)} Public Override Object ProvideValue (IServiceProvider serviceProvider) {if (_converter == zero) _converter = New MyConverter (); Return converter; }

}

You end up with a syntax like this:

  {binding converter = {conversion: MyConverter}}  

This approach is an added benefit to ensuring that all your converters are singleton.

This does a great job of explaining the concept and providing sample code.


Comments