30

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"
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
Rhys
  • 2,807
  • 8
  • 46
  • 68

6 Answers6

61

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.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Shebin
  • 3,310
  • 2
  • 24
  • 27
  • 4
    This didnt work for me when my string was `{0} Complete` I had to use the answer below `{}{0} Complete` – JKennedy Apr 23 '15 at 10:45
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">
H.B.
  • 166,899
  • 29
  • 327
  • 400
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>
grantnz
  • 7,322
  • 1
  • 31
  • 38
4

Hi You can write as following:

<TextBlock>
     <TextBlock>This is </TextBlock>
     <TextBlock Text="{Binding Title}"></TextBlock>
</TextBlock>
3

just use StringFormat for formatting purpose.

<TextBlock Text="{Binding Title,StringFormat='This is {0}'}" Foreground="#FFC8AB14" FontSize="28" />
Kishore Kumar
  • 21,449
  • 13
  • 81
  • 113
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.

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129