TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
XAML Coding Standard
Search

VAL$002

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, "mcn" denotes "my custom namespace").

First, define a resource for your custom converter:

  1. <:mcn:MyCustomConverter x:Key="MyCustomConverter"/>
  2. Next, use your custom convertor:

  3. SomeProperty="{Binding
          SomeBindableProperty,
          Converter={StaticResource MyCustomConverter}
    }"

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:

  SomeProperty="{
    Binding SomeBindableProperty
    Converter={mcn:MyCustomConverter}
}"

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.