Wednesday, March 23, 2011

Value Converter - Part I : Silverlight

              In Silverlight, when you’re binding data to controls there will be times when the data needs to be modified when it is binded to the control or when it goes back to the source property (during TwoWay binding).  You can always write code to change a given value, but in many cases it’s much easier to write a simple value converter instead that can be re-used. Although a separate property can be created in the source object that handles the formatting, a value converter can be created and re-used. To create a ValueConverter, first you need to add a class to your silverlight project and implement an interface IValueConverter. The IValueConverter interface defines two members Convert and ConvertBack. Convert is used to convert the data when it is propagating from the binding source to binding target i.e. from the source object to the control. ConvertBack work the other way round. 

IValueConverter Interface:
The interface provides a way to apply custom logic to a binding. If you want to associate a value converter with a binding, create a class that implements the IValueConverter interface and then implement the Convert and ConvertBack methods. Converters can change data from one type to another, translate data based on cultural information, or modify other aspects of the presentation.
Value converters are culture-aware. Both Convert and ConvertBack methods have a culture parameter that indicates the cultural information. If cultural information is irrelevant to the conversion, then you can ignore that parameter in your custom converter.
The Convert and ConvertBack methods also have a parameter called parameter so that you can use the same instance of the converter with different parameters. For example, you can write a formatting converter that produces different formats of data based on the input parameter that you use.
IValueConverter.Convert Method:
This method is called when a value propagates from the binding source to the binding target.
Object Convert (Object value, Type targetType,Object parameter, CultureInfo culture)
value (System.Object): The value from the binding source.
targetType (System.Type): The type of the binding target property.
parameter (System.Object): The converter parameter to use.
culture (System.Globalization.CultureInfo): The culture to use in the converter.
Return Value (System.Object): A converted value.
  
IValueConverter.ConvertBack Method:
This method is called when a value propagates from the binding target to the binding source. The implementation of this method must be the reverse of the Convert method.
value (System.Object): The value from the binding source.
Object ConvertBack (Object value, Type targetType,Object parameter, CultureInfo culture)

targetType (System.Type): The type of the binding target property.
parameter (System.Object): The converter parameter to use.
culture (System.Globalization.CultureInfo): The culture to use in the converter.
Return Value (System.Object): A converted value.
In the next part we will see an example for Value Converter.

Hope this helps. Happy Coding !!!


Microsoft Silverlight 4 Data and Services Cookbook          .NET 3.5 Wrox Box: Professional ASP.NET 3.5, Professional C# 2008, Professional LINQ, .NET Domain-Driven Design with C#     Silverlight 4 in Action

0 comments: