Using a value converter involves setting the Converter property of a Binding object. This property determines which IValueConverter to use when transforming data. By default, this property isn't set to anything (null), but you can set itxe "Converter property" to reference an IValueConverter you've created. Before you can reference an IValueConverter, you must add it as a resource. You can reference an IValueConverter by first adding it to the Resources collection, as shown in listing 2: Listing 2 Referencing a Value Converter as a Page Resource <UserControl x:Class="ValueConverterExample.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ValueConverterExample" Width="400" Height="300"> <UserControl.Resources> <local:YesNoValueConverter x:Key="myConverter" /> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White"> <TextBlock x:Name="myTextBlock" Text="{Binding IsCorrect, Converter={StaticResource myConverter}}" /> </Grid> </UserControl> This shows how to introduce a custom value converter to the XAML world. The local prefix is assumed to be defined as a namespace. The key myConverter is used to reference the YesNoValueConverter in XAML. The following is an example of referencing a value converter: <TextBlock x:Name="myTextBlock" Text="{Binding IsCorrect, Converter={StaticResource myConverter}}" /> This example shows a basic Binding that uses a custom converter. This converter alters the displayed text of a bool property called IsCorrect. The example shows that the custom converter is referenced through the Converter property. This property uses the curly-brace markup extension syntaxxe "curly braces:syntax" just like the Binding syntax because it's the syntaxxe "binding:syntax" used to reference a resource. You can also pass a custom parameter or the culture information if you need to. Tip: A statement such as the binding statement shown in the previous example can seem to be a jumble of curly braces. Think of each matched set of braces as a separate statement, substituted in when parsed and evaluated. For example, the {StaticResource myConverter} statement is a complete StaticResource markup extension statement itself, the result of which, after evaluation, is passed in to the Converter parameter of the Binding statement. The Binding class exposes an object property called ConverterParameterxe "ConverterParameter property", which can be used to pass an arbitrary value to an IValueConverter. The value converter uses the value of the ConverterParameter in the Convert and ConvertBack methods. By default, this value is null but you can use it to pass along any data you want, such as a format string or an index. If you need to pass along culture-related data, we recommend using the onverterCulture propertyxe "ConverterCulture property". The ConverterCulture property of the Binding class allows you to set the culture. This culture is passed along as a CultureInfoxe "CultureInfo" object that can be used by the Convert and ConvertBack methods. By default, the CultureInfo object reflects the value of the Language attribute of the calling FrameworkElement. The Language attribute is used for localization and globalization. This value uses a string that defaults to en-US, which represents U.S. English.
Value converter tricks
Value converters are powerful and allow you to extend binding to support scenarios not natively supported in Silverlight or let you manipulate object data for objects that may otherwise have schemas you can't touch.
For example, a colleague created a value converter that has a field name as a parameter and then implements binding to a dictionary of fields, much like a DataSet. At the time, Silverlight had no support for binding to indexed values or any support for dynamic properties, so this was a huge time-saver and allowed us to use existing business objects (which included a dictionary of additional values) in case we couldn't alter the implementation of the existing objects.
Since MultiBinding (the ability to bind two fields to a single control) isn't supported in Silverlight, in another instance we used a purpose-built value converter to combine all the address fields in an object into a single string to be displayed in a grid column. In that case, the binding source was the entire object and the value converter looked for specific fields in that object. The ConvertBack method was left empty in that case, since it supported only OneWay binding.
Though you don't want value converters to be the solution to all your binding woes (in many cases, an alternate design may serve you better), they're powerful enough to provide lots of options in situations where you may otherwise be tempted to write a bunch of code in your code-behind. Creating and using a value converter can be valuable when working with data, as shown with our basic yes/no example. Value converters can be useful in even more complex scenarios. For instance, Silverlight doesn't have support for HTML tags in regular text controls, so you may consider using a value converter to scrub the HTML tagsxe "HTML:tags, no support" from a string before binding it to your UI. Value converters were often used to format values for binding. We've already seen a way to format strings for display. Let's now look at how to handle fallback values and null value display.