Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Binding within text literal

Tags:

c#

wpf

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.

like image 885
Adam Rackis Avatar asked Jul 15 '11 14:07

Adam Rackis


3 Answers

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.

like image 136
Ross Avatar answered Nov 18 '22 06:11

Ross


You're looking for the StringFormat property of Binding.

Text="{Binding CurrentUser, StringFormat=Hello {0}}"
like image 39
Etienne de Martel Avatar answered Nov 18 '22 07:11

Etienne de Martel


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

should do.

like image 32
Vlad Avatar answered Nov 18 '22 07:11

Vlad