3

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!

1
  • Thanks a lot! Both of them worked :D Commented Nov 7, 2014 at 18:01

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

You can do it like this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.