Adding text to a bound TextBlock

Rhys picture Rhys · Aug 20, 2011 · Viewed 25k times · Source

I would like to prepend a text in a data-bound text block:

<TextBlock Text="{Binding Title}" />

The text that is shown is:

"My title"

What I want to be shown is:

This is "My title"

Answer

Shebin picture Shebin · Aug 20, 2011

You can use the StringFormat property of the binding:

 <TextBlock Text="{Binding Title, StringFormat=This is {0}}"></TextBlock> 

Check out this blog post for more information: WPF String.Format in XAML with the StringFormat attribute.