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.

To create a value converter, you must create a class that implements the IValueConverterxe "IValueConverter" interface, which enables you to create some custom logic that transforms a value. This transformation may take place in one of two methods depending on the flow of your data. The first method, Convertxe "Convert method", is used when the data is moving from the source to the target - for example, from your object to a TextBox. If the data is flowing from the target back to the source, such as when the value entered in a TextBox goes back to your object, a method called ConvertBackxe "ConvertBack method" is used. Both methods are members of the IValueConverter interface. This interface and its methods are demonstrated in listing 1.

Listing 1 A value converter that converts a Boolean to "
Yes" or "No" (C#)

public class YesNoValueConverter : IValueConverter
{
  public object Convert(object value, Type targetType,             #A
    object parameter, System.Globalization.CultureInfo culture)
  { 
    bool isYes = bool.Parse(value.ToString());
    if (isYes)
      return "Yes";
    else
      return "No";
  }
public object ConvertBack(object value, Type targetType,            #B
    object parameter, System.Globalization.CultureInfo culture) 
  {
    string boolText = value.ToString().ToLower();                         
    if (boolText == "yes")
      return true;
    else if (boolText == "no")
      return false; 
    else
      throw new InvalidOperationException("Please enter 'yes' or 'no'.");
  }
}
#A Convert function
#B ConvertBack function

This listing shows a value converter that converts between a bool and Yes or No. This converter uses the Convert method when data is being bound to your UI. It's this method that converts a bool to Yes or No. When the UI is passing data back to its source (TwoWay bindingxe "binding modes:TwoWay"), the ConvertBack method is used. This method converts Yes to true and No to false. These methods control the conversion process. To assist in this process, both of these methods give you the opportunity to provide custom information.

 Both the Convert and ConvertBack methods allow you to use two optional pieces of information. The first is an arbitrary object called parameter that can be used by your conversion logic. By default, this object will be null, but you can set it to any value that you find useful. The other piece of information specifies the CultureInfoxe "CultureInfo" object to use when converting the values. We'll discuss both parameters in a moment. But, to set the CultureInfo or pass along a custom parameter, you first must know how to use a value converter from markup.

 

Total Pages : 8 34567

comments