O.O
O.O

Reputation: 11307

What is the purpose of a value converter in Silverlight?

I'm just starting to get my hands dirty with silverlight and "value converter" keeps popping up. I don't remember reading about them in the ASP.NET web app world. Is this something special in Silverlight/WPF? What is its purpose?

Thanks!

Upvotes: 0

Views: 155

Answers (2)

Nilesh Gule
Nilesh Gule

Reputation: 1621

There is stark contrast between the web platform and the Windows when it comes to data binding. Especially in WPF / Silverlight / Windows Phone 7. These technologies support databinding differently compared to Web which is stateless.

A very common example of a value convertor is when you want to hide or show a control in WPF / Silverlight. Controls like stack panel have visibility property which is an enum. We can assign values like Visible / Collapse / Hidden to show or hide the stack panel. In most cases the visibility is controlled by a boolean value. So you use a convertor to convert the boolean to visibility.

Another example of value convertor could be formatting of amount fields. Say you want to display 1000 which is stored in the database as $1,000.00 in an amount text box. You can use the value convertor to do so.

The possibilities are endless. You can think of value convertor as a visual representation of something. Another example I can think of is the completion progress of any task. You can show a nice colourful progressbar instead of showing values like 10%, 20%, 30% completed :)

Hope this helps.

Upvotes: 1

Derek Beattie
Derek Beattie

Reputation: 9478

From this excellent post:

When you’re binding data to controls there will be times when the data needs to be modified or tweaked some on the way into a control or as the data leaves a control and goes back to the source property (during a TwoWay binding for example).  Sure, 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.  In this post I’ll walk through creating a value converter and then show the code for a few of the value converters I find myself using fairly frequently.

Upvotes: 2

Related Questions