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">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With