WPF Control content with curly braces

For some people it is obvious but for me it was quite a discovery when I’ve found out how to insert curly braces into a Text or Content of a control. Surely you can’t simply write:

<Button Content="{Curly braces here}"/>

because in XAML, braces identify markup extension syntax and you would get a compiler error ( The tag 'Curly' does not exist in XML namespace…).

Instead you should prefix the content with another pair of braces. Here is an example:

<StackPanel>
<Button Content="{}{Curly braces here}"
Margin="10" Width="120" />
<TextBox Text="{}{Curly braces there}"
Margin="10" Width="120"/>
<Label Content="{}{I can put them everywhere! ;-)}"
Margin="10" Width="180"/>
</StackPanel>

And the result looks like that:

CurlyBraces

I recommend reading WPF Recipes in C# 2008: A Problem-Solution Approach which has tons of this kind of useful recipes.

Monika

No Comments