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.

When writing code, you can format values using the string.Format function. But until Silverlight 4, there was no good way to do the equivalent during a binding operation. You could write a custom value converter, but that gets old quickly and becomes another testing and maintenance point.

Silverlight 4 introduced the ability to use string formattingxe "data binding:string format" when binding. The syntax is essentially the same as the string.Format function. For example, this will set the value of the TextBlock to be "DOB: May 18, 2007" assuming the DateOfBirth property on your binding source contains the value 5/18/2007:

<TextBlock Text="{Binding DateOfBirth, StringFormat=DOB:\{0:D\}}" />

Similarly, this binding expression will set the value of the TextBlock to be $1,024.10 assuming the decimal BilledAmount field contains the value 1024.10m:

<TextBlock Text="{Binding BilledAmount, StringFormat=\{0:C\}}" />

Sometimes, simply formatting the value isn't enough. In those cases, you may need to perform a real data conversion and write your own custom value converter.

Total Pages : 8 12345

comments