WPF XAML Markup – How Do I Preserve Whitespace Characters?

An issue you’re likely to run into in XAML (WPF, XBAP, and SIlverlight applications) is the need to keep whitespace characters intact in your markup.  By default, XML collapses all whitespace, ignoring strings of spaces, tabs, and return characters inside an XML node, which it graciously converts to a single space.

If you want to include a series of spaces in your XML node markup, you can add a xml:space=”preserve” attrbute to your XML elements.  This attribute  is part of the XML standard.  If you turn it on, it preserves all whitespace appearing inside the XML node, including any hard return characters included inside the XML node.

<Button Name="btnSample" xml:space=preserve">Respects my      spaces
and carraige return</Button>
  del.icio.us it! digg it! reddit! technorati! yahoo!
Advertisements

One response to “WPF XAML Markup – How Do I Preserve Whitespace Characters?

  1. or alternatively, use the buttons Content property. The spaces would be enclosed in the quotes for the text 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s