I'm trying to format a string in WPF for a text block. I want to obtain something like this in my text block: [name]. I know to bind text block's text to the string property which I want to show but I don't know how to put the brackets.
Can you help me?
Thanks!
-
Thanks a lot! Both of them worked :DStefi Pallo– Stefi Pallo2014-11-07 18:01:21 +00:00Commented Nov 7, 2014 at 18:01
Add a comment
|
2 Answers
Put it in the resources as such, and then access the format in the StringFormat attribute of the binding.
<Page.Resources>
<system:String x:Key="InBracketsFormat">[{0}]</system:String>
</Page.Resources>
<TextBlock
Text="{Binding MyValue, StringFormat={StaticResource InBracketsFormat}}"/>
This method provides the advantage of reuse as well as being able to have keyword tokens (such as the ' character) within the text for the format.