Binding only part of a label Binding only part of a label wpf wpf

Binding only part of a label


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>    <Label.Content>        <Binding Path="QuestionnaireName"/>    </Label.Content>    <Label.ContentStringFormat>        Thank you for taking the {0} questionnaire    </Label.ContentStringFormat></Label> 

The same as short Version:

<Label Content="{Binding QuestionnaireName}" ContentStringFormat="Thank you for taking the {0} questionnaire" />

Using it to display a unit after the value:

<Label Content="{Binding Temperature}" ContentStringFormat="{}{0}°C" />

While this sample will not:

<Label>    <Label.Content>        <Binding Path="QuestionnaireName" StringFormat="Thank you for taking the {0} questionnaire"/>    </Label.Content>            </Label>


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]


Often overlooked is simply chaining multiple textblocks together for example

<TextBlock Text="{Binding FirstName}" /><TextBlock Text=" " /><TextBlock Text="{Binding LastName}" />