XAML special characters and white spaces
XAML special characters and white spaces
Some beginner stuff i found a lot of people do not know about.
XAML is based upon the rules of XML. What this means is that characters such as ‘&’, ‘<’, ‘>’ and ‘”’ have their own meaning in XML and the XAML parser will understand them differently then what you intended them for.
If you wish your button’s content to look like this <I am a Button> then you must change your XAML text to <Button Content="<I am a Button>"></Button> which creates a button like this:
Other character replacements are (don't forget the ‘;’) :
- & that replaces ‘&’.
- " that replaces ‘”’.
You also need to pay attention to white spaces. XML, by default, collapses all white spaces, carriage returns and tabs into a single space character if they appear between text and will eliminate the spaces completely before or after the text.
For example, the following XAML:
<TextBox>
carriage return space and some more " ".
</TextBox>
will be shown as:
To fix this, we need to use the xml:space="preserve" which is set in the element level of the control like this:
<TextBox xml:space="preserve">
carriage return space and some more " ".
</TextBox>
Now our text box looks like this:
