Take the 2-minute tour ×

How would one achieve mixing bound values with constant text in a WPF bound control?

For example, say I have a form displaying orders, and I want a label that displays text like "Order ID 1234".

I've tried things like:

text="Order ID {Binding ....}"

Is this achievable, or do I have to do something like having more than one label in a flow control?

share|improve this question
add comment

4 Answers

up vote 19 down vote accepted

If you're using 3.5 SP1, you can use the StringFormat property on the binding:

<Label Content="{Binding Order.ID, StringFormat=Order ID \{0\}}"/>

Otherwise, use a converter:

<local:StringFormatConverter x:Key="StringFormatter" StringFormat="Order ID {0}" />
<Label Content="{Binding Order.ID, Converter=StringFormatter}"/>

With StringFormatConverter being an IValueConverter:

[ValueConversion(typeof(object), typeof(string))]
public class StringFormatConverter : IValueConverter
    public string StringFormat { get; set; }

    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture) {
         if (string.IsNullOrEmpty(StringFormat)) return "";
         return string.Format(StringFormat, value);

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
        throw new NotSupportedException();

That'll do the trick.

[Edit : Change the Text property to Content]

share|improve this answer
With the stringformat example, what happens if the string needs to contain a comma? Do you then have no choice but to go with the converter option? –  Neil Barnwell Oct 13 '09 at 16:58
AFAIK the label control does not have the Text property. You should bind to the Content property –  Eduardo Molteni Sep 7 '10 at 3:25
add comment

The Binding.StringFormat property doesn't work on Labels, you need to use the ContentStringFormat property on the Label.
For example, the following sample will work:

<Label Grid.Row="0" Name="TitleLabel">
        <Binding Path="QuestionnaireName"/>
        Thank you for taking the {0} questionnaire

While this sample will not:

            <Binding Path="QuestionnaireName" StringFormat="Thank you for taking the {0} questionnaire"/>
share|improve this answer
Sigh. Another arbitrary restriction imposed by WPF developers. –  glebd Sep 16 '10 at 13:16
Any idea why Binding.StringFormat does not work? –  Wouter Mar 7 '11 at 14:12
Since Label.Content is of type Object, the binding sees no need to convert and format the value as a string. –  YotaXP Nov 13 '11 at 0:00
This one is really the answer to the question. –  Farzan Aug 23 '13 at 19:57
add comment

Often overlooked is simply chaining multiple textblocks together for example

<TextBlock Text="{Binding FirstName}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding LastName}" />
share|improve this answer
This will be displayed on 3 separate lines. –  Erik Karimov Nov 15 '12 at 16:26
this depends on what kind of stackpanel it's in –  Joris Weimar Dec 16 '12 at 15:19
add comment

Another approach is to use a single TextBlock with multiple Run elements within it:


.. but to bind to a element you need to use add a BindableRun class.

Update But there are some drawbacks to this technique ... see here

share|improve this answer
This one is the choice if you want also a different formatting (number in bold f.e.) –  Sergey Aldoukhov Mar 20 '09 at 23:18
add comment

Your Answer


