In WPF, is there any way to have the Text
property of a TextBlock
to contain both hard coded text and a specific binding?
What I have in mind is something along the lines of the following (ofcourse, the below doesn't compile):
<TextBlock Text="Number of Fans: {Binding Artist.Fans.Count}"></TextBlock>
TextBlock is not editable.
textblock (plural textblocks) The block of pages making up a book, excluding the binding.
The TextBlock control provides flexible text support for UI scenarios that do not require more than one paragraph of text. It supports a number of properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping.
There is, if you are on .Net 3.5 SP1
<TextBlock Text="{Binding Path=Artist.Fans.Count, StringFormat='Number of Fans: {0}'}" />
In using the above approach:
<TextBlock Text="{Binding Path="Artist.Fans.Count, StringFormat='Number of Fans: {0}'}" />
I found it somewhat restrictive in that I couldn't find a way to bold face inside the StringFormat nor could I use an apostrophe in the StringFormat.
Instead I went with this approach, which worked better for me:
<TextBlock TextWrapping="Wrap"> <Run>The value</Run> <Run Text="{Binding Path=MyProperty1, Mode=OneWay}" FontWeight="Bold" /> <Run>was invalid. Please enter it with the format... </Run> <LineBreak/><LineBreak/> <Run>Here is another value in the program</Run> <Run Text="{Binding Path=MyProperty2, Mode=OneWay}" FontWeight="Bold" /> </TextBlock>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With