Adding text to a bound TextBlock
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">
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>
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.