Adding text to a bound TextBlock
Asked Answered
B

6

31

I would like to prepend a text in a data-bound text block:

<TextBlock Text="{Binding Title}" />

The text that is shown is:

"My title"

What I want to be shown is:

This is "My title"
Blayze answered 20/8, 2011 at 9:15 Comment(0)
E
63

You can use the StringFormat property of the binding:

 <TextBlock Text="{Binding Title, StringFormat=This is {0}}"></TextBlock> 

Check out this blog post for more information: WPF String.Format in XAML with the StringFormat attribute.

Exstipulate answered 20/8, 2011 at 10:11 Comment(1)
This didnt work for me when my string was {0} Complete I had to use the answer below {}{0} CompleteSend
M
12

If you want to do it in the binding:

<TextBlock Foreground="#FFC8AB14" FontSize="28">
    <TextBlock.Text>
        <Binding Path="Title">
            <Binding.StringFormat>
                This is "{0}"
            </Binding.StringFormat>
        </Binding>
    </TextBlock.Text>
</TextBlock>

Element syntax required to escape quotes. If the quotes where just to mark the inserted text and should not appear in the output it is much easier of course:

<TextBlock Text="{Binding Title, StringFormat={}This is {0}}" Foreground="#FFC8AB14" FontSize="28">
Mccracken answered 20/8, 2011 at 10:9 Comment(0)
G
5

You could do this with a converter.

<TextBlock Text="{Binding Title, ConverterParameter=This is, Converter={StaticResource TextPrefixConverter}}" Foreground="#FFC8AB14" FontSize="28" />

The converter would simply prefix the bound value with the ConverterParameter.

public class TextPrefixConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {                        
        String result = String.Empty;
        if ( parameter != null)
            result = parameter.ToString( );

        if (value != null)
            result += value.ToString( );

        return result;
    }
...
}

It's not obvious is the spaces and/or quotes are intended to be part of the output. If so, the converter could be changed to trim the spaces and/or add quotes to the constructed string.

Another way of doing this is:

<TextBlock Foreground="#FFC8AB14" FontSize="28" >
    <Run Text="This is " />
    <Run Text="{Binding Path=Title}" />       
</TextBlock>
Gintz answered 20/8, 2011 at 9:59 Comment(0)
H
4

Hi You can write as following:

<TextBlock>
     <TextBlock>This is </TextBlock>
     <TextBlock Text="{Binding Title}"></TextBlock>
</TextBlock>
Hermineherminia answered 20/8, 2011 at 9:44 Comment(0)
E
3

just use StringFormat for formatting purpose.

<TextBlock Text="{Binding Title,StringFormat='This is {0}'}" Foreground="#FFC8AB14" FontSize="28" />
Empower answered 20/8, 2011 at 10:11 Comment(0)
E
1

The best approach here in terms of performance, as already answered, is using StringFormat for Binding and assign it to the Text property of TextBlock.

However if performance isn't a concern, and XAML readability is preferred, another approach is to use Run inside TextBlock:

<TextBlock Foreground="#FFC8AB14" FontSize="28">
    This is <Run Text="{Binding Title}" />
</TextBlock>

Also, this way you can apply different styles (text/background color, italic/bold font, font size, etc.) to different parts of your TextBlock, which is something you can't do with Binding's StringFormat. And this is way more efficient than having multiple TextBlocks with different text/background styles.

Ensepulcher answered 25/1, 2017 at 17:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.