69

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>
Emond
  • 50,210
  • 11
  • 84
  • 115
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360

6 Answers6

126

There is, if you are on .Net 3.5 SP1

<TextBlock Text="{Binding Path=Artist.Fans.Count, 
                 StringFormat='Number of Fans: {0}'}" />
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Scott Weinstein
  • 18,890
  • 14
  • 78
  • 115
45

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>                    
CAD bloke
  • 8,578
  • 7
  • 65
  • 114
doogie
  • 591
  • 4
  • 4
6

Use Binding.StringFormat:

<TextBlock Text="{Binding Artist.Fans.Count, StringFormat='Number of Fans: {0}'}"/>
Danko Durbić
  • 7,077
  • 5
  • 34
  • 39
5

Here the binding value(clouds.all) is added with "%". You can add any value you want after "\{0\}".

 <TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}%}"/>
reza.cse08
  • 5,938
  • 48
  • 39
  • Could you please [edit] in an explanation of why this code answers the question? Code-only answers are [discouraged](http://meta.stackexchange.com/q/148272/274165), because they don't teach the solution. – Nathan Tuggy Oct 29 '15 at 02:33
  • @Nathan I edit my answer. Is it helpful now? Thank for your advice. – reza.cse08 Oct 30 '15 at 08:27
1

With XAML using Template 10 and MVVM:

Just to be clear:

  • By definition, binding binds values to properties of controls.
  • Under the MVVM paradigm as implemented in the 'Template 10' framework, the values are initialized in the ViewModel associated to the relevant XAML page.

Here is how to have hardcoded text together with a binding in a Text property:

    <Page
        ...
        xmlns:vm="using:doubleirish.ViewModels"
        xmlns:sys="using:System"
        xmlns:controls="using:Template10.Controls"
        ...

        <Page.DataContext>
            <vm:StocksViewModel x:Name="ViewModel" />
        </Page.DataContext>

        ...

        <controls:PageHeader ... Text="{x:Bind sys:String.Format('Ticker : {0}', ViewModel.Ticker)}">

    ...

    </Page>
Varus Septimus
  • 1,510
  • 20
  • 13
0

The solution that worked for me:

<Label Content="{Binding Artist.Fans.Count}" ContentStringFormat="Number of {0}"/>