Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you insert a binding into the middle of a sentence of a TextBlock in WPF?

I'm looking for something along these lines:

<TextBlock
    Grid.Column="1"
    Text="Welcome, {Binding UserName}!" />

This will of course actually display the text "{Binding UserName}" to the user rather than decode it, but I know you can do something like this with ASP.NET, so I'm hoping there's a way to get this to work in WPF.

I'm already aware that I could use an IValueConverter...I'm looking for something I can do purely in markup if possible.

EDIT:

Based on @Matt Hamilton's most excellent solution, I attempted to push the envelope and bind two values into the same TextBlock using a MultiBinding. Works like a charm:

<TextBlock
    Style="{StaticResource TextBlock_ValueStyle}"
    Grid.Column="1">
    <TextBlock.Text>
        <MultiBinding
            StringFormat="{}Attempts: {0:G} of {1:G}">
            <Binding
                Path="AttemptNumber" />
            <Binding
                Path="AttemptCount" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

This produces: Attempts: 1 of 4 (assuming AttemptNumber = 1 and AttemptCount = 4).

I also found this link helpful for figuring out which formats to place after the colon:

http://msdn.microsoft.com/en-us/library/fbxft59x.aspx

like image 253
devuxer Avatar asked Sep 09 '09 22:09

devuxer


People also ask

How do I create a line break in TextBlock WPF?

Adding Line Breaks You can do this with a LineBreak inline, added as an XAML element within the text. A new line will be started at the position of this element. If you have configured the text to be justified, the final line before the line break will not be expanded to fill the available width.

What is the syntax for binding a command to an object?

Binding path syntax. Use the Path property to specify the source value you want to bind to: In the simplest case, the Path property value is the name of the property of the source object to use for the binding, such as Path=PropertyName . Subproperties of a property can be specified by a similar syntax as in C#.

What does binding mean in XAML?

Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.

What is text block in WPF?

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.


1 Answers

You can use the StringFormat binding property in .NET 3.5 SP1:

<TextBlock Text="{Binding UserName,StringFormat='Welcome, \{0\}!'}" />

Note that you need to escape the curly braces in the string format with a backslash.

Update Yes, multiple values are also supported:

<TextBlock>
    <TextBlock.Text>
        <MultiBinding StringFormat="Welcome, {0} {1}!">
            <Binding Path="FirstName" />
            <Binding Path="LastName" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>
like image 150
Matt Hamilton Avatar answered Nov 13 '22 15:11

Matt Hamilton