So I have a TextBlock
bound to a property on my object like so:
<TextBlock Grid.Column="1" Text="{Binding FriendlyName}" Margin="0,5,0,5"/>
I would now like to surround this text in quotes and add a hyphen to it so I tried:
<TextBlock Grid.Column="1" Text="{Binding FriendlyName, StringFormat= - \"{0}\"}" Margin="0,5,0,5"/>
But got a number of errors.
I also tried (from here):
<TextBlock Grid.Column="1" Text="{Binding FriendlyName, StringFormat= - "{0} "}" Margin="0,5,0,5"/>
but got the error:
Error 4 Names and Values in a MarkupExtension cannot contain quotes. The MarkupExtension arguments ' FriendlyName, StringFormat= - "{0} "}' are not valid
So I was wondering is it possible to add quotes to a binding using StringFormat
?
You should add single quotes:
<TextBlock Grid.Column="1" Text="{Binding FriendlyName, StringFormat='- "{0}"'}" Margin="0,5,0,5"/>
Or you can use MultiBinding
:
<TextBlock Grid.Column="1" Margin="0,5,0,5">
<TextBlock.Text>
<MultiBinding StringFormat=" - "{0}"">
<Binding Path="FriendlyName" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
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