Synopsis: | Consider enhancing your custom value converters with markup extensions |
Language: | XAML |
Severity Level: | 1 |
Category: | Value Converters |
Description: |
To use a custom value converter in XAML, you usually have to define a resource for it, and use the resource key to reference your converter, as illustrated by the following XAML code snippets (here, " First, define a resource for your custom converter:
Next, use your custom convertor: As you can see, this leads to rather verbose XAML. If, on the other hand, you implement a markup extension that enhances your custom converter by providing a static instance of your converter, then the XAML becomes less verbose. First of all, the markup extension eliminates the need to define a resource for your custom converter: line (1) from the example above is no longer needed. Second, the syntax for the binding that uses the converter becomes simpler: ✔ There are various ways to implement a markup extension for your custom converters. You can either let your converter class derive from MarkupExtension, while still implementing one of the value conversion interfaces, IValueConverter or IMultiValueConverter; or you can restrict the role of your converter class to implementing one of the value conversion interfaces, and create an additional class to act as the markup extension for your converter. |