Having hardcoded text with a binding in a TextBlock
Asked Answered
I

6

71

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>
Identify answered 25/4, 2009 at 16:6 Comment(0)
J
128

There is, if you are on .Net 3.5 SP1

<TextBlock Text="{Binding Path=Artist.Fans.Count, 
                 StringFormat='Number of Fans: {0}'}" />
Just answered 25/4, 2009 at 16:14 Comment(2)
Is this possible to use multiple outputs, similar to args[] in the string.Format([1], [2], [3],...[n])?Inwrought
it's missing escaping \{0\}Gautama
C
46

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>                    
Caye answered 12/12, 2012 at 22:10 Comment(1)
... or use 2 bindings in the same TextBlockHelminthic
D
6

Use Binding.StringFormat:

<TextBlock Text="{Binding Artist.Fans.Count, StringFormat='Number of Fans: {0}'}"/>
Dove answered 25/4, 2009 at 16:16 Comment(1)
hi Danko - would you know how to make it work with property element syntax?Alvord
A
6

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\}%}"/>
Armentrout answered 28/10, 2015 at 7:0 Comment(2)
Could you please edit in an explanation of why this code answers the question? Code-only answers are discouraged, because they don't teach the solution.Buckram
@Nathan I edit my answer. Is it helpful now? Thank for your advice.Armentrout
B
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>
Bathypelagic answered 18/12, 2018 at 21:51 Comment(0)
P
0

The solution that worked for me:

<Label Content="{Binding Artist.Fans.Count}" ContentStringFormat="Number of {0}"/>
Preamplifier answered 26/12, 2022 at 14:49 Comment(1)
But your solution is for Label, not TextBlock as asked in the question.Nagari

© 2022 - 2024 — McMap. All rights reserved.