WPF’s Binding
and MultiBinding
types define a Converter
property that allows you to provide a converter that will be used during the binding process. If you don’t explicitly specify a value converter, WPF will use its built-in primitive ones behind the scenes (such as DefaultValueConverter
, ObjectTargetConverter
and SystemConvertConverter
– all are internal
types defined in the MS.Internal.Data
namespace).
If you do specify a value converter, WPF will invoke methods on your converter during the data binding process. These methods are:
Convert()
: called to convert a value when propagating it from the binding source to the binding target.ConvertBack()
: called to convert a value when propagating it from the binding target to the binding source.
The following diagram depicts this process:
What I have found whilst working with WPF is that value converters (shown in the middle of the above diagram) are often needed to solve similar problems. This code library seeks to address the most common converter requirements and alleviate the need for a lot of custom converter code in any WPF (or Silverlight) application.
The following converters are included in this project:
Name | Supports Singular Conversions | Supports Multiple Conversions (WPF only) | Supports Forward Conversions | Supports Backward Conversions | Markup Extension Support |
---|---|---|---|---|---|
BooleanToVisibilityConverter | Yes | No | Yes | Yes | WPF, SL5 |
CaseConverter | Yes | No | Yes | Yes | WPF, SL5 |
ConverterGroup | Yes | No | Yes1 | Yes2 | None |
DateTimeConverter | Yes | No | Yes | Yes | WPF, SL5 |
ExpressionConverter | Yes | Yes | Yes | No | WPF, SL5 |
FormatConverter | Yes | Yes | Yes | Yes3 | WPF, SL5 |
MapConverter | Yes | No | Yes | Yes | None |
MultiConverterGroup | No | Yes | Yes4 | Yes5 | None |
TypeConverter | Yes | No | Yes | Yes | WPF |
1Assuming all child converters support forward conversions.
2Assuming all child converters support backward conversions.
3Only for singular conversions.
4Assuming all child converters support forward conversions.
5Assuming all child converters support backward conversions.
The links in the above table provide a useful overview of each converter. Please note that the API documentation for each converter provides further examples and detail. In order to use the converters in your application, you must:
- Add a reference to the Kent.Boogaart.Converters.dll assembly.
- Declare an XML->CLR namespace mapping to
http://schemas.kent.boogaart.com/converters
.
All the examples in this section assume a declaration such as:
<Window xmlns:con="http://schemas.kent.boogaart.com/converters">