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"
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"
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.
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>
Hi You can write as following:
<TextBlock>
<TextBlock>This is </TextBlock>
<TextBlock Text="{Binding Title}"></TextBlock>
</TextBlock>
just use StringFormat for formatting purpose.
<TextBlock Text="{Binding Title,StringFormat='This is {0}'}" Foreground="#FFC8AB14" FontSize="28" />
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 TextBlock
s with different text/background styles.
© 2022 - 2024 — McMap. All rights reserved.
{0} Complete
I had to use the answer below{}{0} Complete
– Send