Showing posts with label value converter. Show all posts
Showing posts with label value converter. Show all posts

Wednesday, March 23, 2011

Value Converter - Part II : Silverlight

This post is the Part II of Value Converter. The Part I can found here

http://www.silverdigita.com/2011/03/value-converter-part-i-silverlight.html


In part I, we saw about the interface IValueConverter and the members of the interface i.e. Convert and ConvertBack. Now let’s see an example. In this example, we will see how to format the decimal value. First of all, let’s create a class that implements IValueConverter interface (let’s call this class Formatter).

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.