It’s often necessary to modify or format some of the raw data received by your data tier application or other data sources. In Silverlight, you can create your own value converter and introduce your own formatting rules. In this blog post I will create a simple value converter that formats double values, and then use it with a RadComboBox.
First, let’s create a class that will represent our data source. The equivalent of this class in more realistic scenario would be a proxy class generated by your data tier application in case you use RIA services.
The Item class can be any class that implements the INotifyPropertyChanged interface. In this case the Item class has only two properties Name and Price.
To data bind the data source class we have just created, you should set the DataContext property of RadComboBox like this:
Since we want to bind our RadComboBox to the items that are contained in Data, not the Data class itself, we can either use the DisplayMemberPath, a property of RadComboBox, or create an ItemTemplate. Otherwise, if you just simply set the DataContext to the data source you will end up with a ToString() representation of your Data class.
To have our two properties, Name and Price, appear in a single ComboBoxItem we should create a custom template. Here is a sample implementation of this:
The result of running this code should be similar to this:
Obviously, we cannot distinguish whether the numbers part of the combobox item are prices or telephone numbers. What we would like to achieve is simply format the UI representation of this values without modifying the data source itself. In Silverlight and WPF you can use the IValueConverter interface to introduce your own format logic. The interface has two virtual methods that you should implement: Convert and ConvertBack.
Here is a simple implementation of a converter that formats the Price property of the data source class to its currency representation.
The Convert method is the place to introduce you custom logic. The ConvertBack method should do the inverse of what Convert does, but Silverlight isn't making use of it, so you can leave it not implemented. Note that the data binding engine doesn’t catch exceptions by custom converters. In order to anticipate any problems with the conversion process of your data type, you should return DependencyProperty.UnsetValue. Now you can declare the value converter in the resources tag of your control like this:
You should also associate your custom converter to the property you want to use it.
Now if you run the application your RadComboBox should look similar to this:
It’s important to note that you can use the value converters in any control in your Silverlight application. Value converters are also very useful when dealing with DateTime types.