Just found out some nice tricks on WPF, so here are some quick hints about using String.Format() with multiple parameters on XAML for WPF (and possibly Silverlight).

Say that you have a Phone class that contains three properties: CountryCode, AreaCode and Number. The DataContext for your Window contains a property of type Phone, and you want to print that object information in a pre-formatted way defined at the XAML file. The desired format is something like +1 55 55555555 (plus sign, followed by CountryCode, followed by a space, followed by AreaCode, followed by a space, followed by Number).

So instead of creating a property to hold the pre-formatted string at your class side, you just want to define it at XAML level.

Take the code below as an example:

<TextBlock>
	<TextBlock.Text>
		<MultiBinding StringFormat="+{0} {1} {2}">
			<Binding Path="Phone.CountryCode" />
			<Binding Path="Phone.AreaCode" />
			<Binding Path="Phone.Number" />
		</MultiBinding>
	</TextBlock.Text>
</TextBlock>

That will just do the trick for you!

However, we have some considerations to pay attention at. First of all:

<MultiBinding StringFormat="+{0} {1} {2}">

That line just works because of that trailing plus sign. If you were to use no plus sign (or any other prefixing string) at all and just display CountryCode directly, then you need a small tweak:

<MultiBinding StringFormat="{}{0} {1} {2}">

That’s the {} just before {0} parameter.

Furthermore, there are some nice attributes to add to the Binding element. For instance, if the value of your DataContext Phone object can’t be retrieved in design-time, you might see {DependencyProperty.UnsetValue} displayed on your design screen. TargetNullValue and FallbackValue can be added to fix that.

<TextBlock>
	<TextBlock.Text>
		<MultiBinding StringFormat="+{0} {1} {2}">
			<Binding Path="Phone.CountryCode" TargetNullValue="--" FallbackValue="??" />
			<Binding Path="Phone.AreaCode" TargetNullValue="--" FallbackValue="??" />
			<Binding Path="Phone.Number" TargetNullValue="--------" FallbackValue="????????" />
		</MultiBinding>
	</TextBlock.Text>
</TextBlock>

The first one is the substitution for null values, while the value of the second one is returned when no value can be found (such as when the bound property is not initialized in design-time). Just take care as that works on runtime as well!

Just some tricks particularly interesting during MVVM development. I hope it can be of some help!