WPF Text StringFormat TextBlock

I am trying to format a string in WPF for a text block. I want to get something like this in your text box: [name]

. I know to bind the text of the text block to the string property I want to show, but I don't know how to put the brackets. Could you help me? Thank!

+3


source to share


2 answers


Put it in resources as such and then access the format in the StringFormat

binding attribute .

<Page.Resources>
    <system:String x:Key="InBracketsFormat">[{0}]</system:String>
</Page.Resources>


<TextBlock
        Text="{Binding MyValue, StringFormat={StaticResource InBracketsFormat}}"/>

      



This technique provides the benefit of reuse as well as the ability to have text labels (such as a character '

) in the text for the format.

+2


source


You can do it like this:



<TextBlock Text="{Binding MyProperty, StringFormat='[{0}]'}"/>

      

+2


source







All Articles