FREE BOOK

Chapter 15: Customizing the Display

Posted by Manning Publication Free Book | Silverlight January 19, 2012
Silverlight has everything you need to format display values, convert both inbound and outbound values, provide special handling for null values and even provide fallbacks for cases when binding fails. Throughout this book, you'll see how to customize the visual representation of your data using these powerful features.

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

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.

Total Pages : 8 45678

comments