开发者

WPF Binding within text literal

开发者 https://www.devze.com 2023-03-21 01:03 出处:网络
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\"

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.开发者_开发问答


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.


You're looking for the StringFormat property of Binding.

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


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

should do.

0

精彩评论

暂无评论...
验证码 换一张
取 消