Having hardcoded text with a binding in a TextBlock Having hardcoded text with a binding in a TextBlock wpf wpf

Having hardcoded text with a binding in a TextBlock


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>                    


Use Binding.StringFormat:

<TextBlock Text="{Binding Artist.Fans.Count, StringFormat='Number of Fans: {0}'}"/>