You probably have had the need to sort an ObservableCollection at some point in one […]
CSLA: Make GUIDs required with a custom validation rule
If you use CSLA and use GUIDs as your unique identifiers you may have need […]
Debugging XAML
Okay, so I have been asked this question more than almost any XAML related question […]
Analyze performance issues in your WPF application with WPFPerf.
Do you have a WPF application that just seems to be slow in some areas […]
The Free Office WPF Ribbon
So you want to use a Ribbon toolbar similar to Microsoft Word in your applications, […]
WPF String.Format in XAML with the StringFormat attribute
So you want to format the output of information but don’t want do it in code behind or write a value converter to do it. Well, good news, you don’t have to. You can format your data directly in XAML. How, you may be asking? New in .NET 3.5 SP1 is the StringFormat attribute.
Example 1: Lets say you want to format a double value into a currency:
Notice the “{ }” just after the StringFormat attribute? What that is doing is escaping the text after the “=” sign. You need to do this because we do not have any text directly after the “=” sign. If you don’t put the “{ }’”, strange things will happen.
And now lets say we want to place some text in front of the currency:
Since we now have text after the “=” sign we no longer need the “{ }”.
How about a date you ask:
Oh, and you want time:
What? You want to create a tooltip comprised of more than one property of an object. Well Okay:
As you can see the StringFormat attribute can be a time saver, and just make life a little easier. One thing to note is that if you use the StringFormat attribute and you bind to a property that has no value, otherwise known as null, then the text that will be displayed is “{DependencyProperty.UnsetValue}”.