Adam Rackis
Adam Rackis

Reputation: 83358

WPF Binding within text literal

Is there any way to do this in a binding expression:

Text="Hello {Binding CurrentUser}"

ie:

<TextBlock HorizontalAlignment="Right" Foreground="#3163AB" Margin="0,0,0,5" 
    FontWeight="Bold" Text="Hello {Binding CurrentUser}" />

Obviously I could break it out into two separate textblocks, but this would be much nicer.

Upvotes: 13

Views: 2438

Answers (3)

Ross
Ross

Reputation: 4568

As of .NET 4, the Text property of a Run can be bound. I use it all the time:

<TextBlock>
    Hello
    <Run Text="{Binding CurrentUser}" />,
    how are you?
</TextBlock>

The StringFormat method is nice, but using a Run with a binding allows the use of Value Converters.

Upvotes: 16

Vlad
Vlad

Reputation: 35584

Text="{Binding CurrentUser, StringFormat=Hello {0}}"

should do.

Upvotes: 6

Etienne de Martel
Etienne de Martel

Reputation: 36852

You're looking for the StringFormat property of Binding.

Text="{Binding CurrentUser, StringFormat=Hello {0}}"

Upvotes: 9

Related Questions