Sunday, May 1, 2011

WPF: Having hardcoded text with a binding in a TextBlock

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>
From stackoverflow
  • There is, if you are on .Net 3.5 SP1

    <TextBlock Text="{Binding Path="Artist.Fans.Count", 
                     StringFormat='Number of Fans: {0}'}" />
    
    Andreas Grech : Excellent, exactly what I needed.
  • Use Binding.StringFormat:

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

0 comments:

Post a Comment