Neil Barnwell
Neil Barnwell

Reputation: 42135

Binding only part of a label

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?

Upvotes: 28

Views: 24982

Answers (6)

LPCRoy
LPCRoy

Reputation: 945

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>

Upvotes: 53

Jogi Joseph George
Jogi Joseph George

Reputation: 699

Modified Mikolaj's answer.

<Label Content="{Binding Order.ID}" ContentStringFormat="Order ID {0}" />

FallbackValue is not a must.

Upvotes: 0

Mikołaj
Mikołaj

Reputation: 99

I found another one approach. @Inferis's solution doesn't work for me and @LPCRoy's isn't elegant for me:

<Label Content="{Binding Path=Order.ID, FallbackValue=Placeholder}" ContentStringFormat="Order ID {0}">

It's my favourite at this moment, it's seems flexible and condensed.

Upvotes: 3

Inferis
Inferis

Reputation: 4662

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]

Upvotes: 25

Rob Walker
Rob Walker

Reputation: 47472

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

<TextBlock><Run>Hello</Run><Run>World</Run></TextBlock>

.. 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

Upvotes: 4

bendewey
bendewey

Reputation: 40245

Often overlooked is simply chaining multiple textblocks together for example

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

Upvotes: 5

Related Questions