Evgeny Pokhilko's Weblog

Programmer's den

Effect of XML:Space=”preserve”


I was working with XAML created by Expression Blend. At runtime I noticed that a TextBlock moved on the right and one line down from the position it was in the designer. It took me some time to find the cause of the discrepancy. Below there is XAML of two TextBlock elements:

<TextBlock Height="17" VerticalAlignment="Top" xml:space="preserve"><Run xml:space="preserve">first line</Run></TextBlock>
<TextBlock Margin="0,22,0,0" Height="23" VerticalAlignment="Top">
    Second line
</TextBlock>

In the designer it looks like this:

When the application is running, the picture is different:

As you see, the second line moved on the right and one line down.

The only unusual part of XAML is the xml:space=”preserve” attribute, which is set at the first TextBlock and the Run elment inside it. This attribute says that all the indentation and spaces in XAML code should be saved at the time of rendering. But according to MSDN the attribute scope is only the content of the element where the attribute is defined. It’s defined at the first TextBlock and the contained Run element. If you remove the attribute from those elements everything renders correctly. But why does the attribute from the first TextBlock influences the second TextBlock. In the debugger I stopped the application and saved XAML of the “second line” TextBlock and it really had xml:space=”preserve” attribute. I assume it somehow came from the “first line” TextBlock.

It is apparently a bug in WPF. The bug only apears If you have more than one nested elements with the xml:space =”preserve” attribute defined at both. The next sibling element after those will have this attribute set to “preserve” implicitly.

The solution is simple: you need to set xml:space=”default” explicitly at the next element. So our XAML will be:

<TextBlock Height="17" VerticalAlignment="Top" xml:space="preserve"><Run xml:space="preserve">first line</Run></TextBlock>
<TextBlock Margin="0,22,0,0" Height="23" VerticalAlignment="Top" xml:space="default">
    Second line
</TextBlock>

Download the code for this post

June 23, 2008 - Posted by | .NET, WPF | , ,

2 Comments »

  1. Evgeny, I did a Google search for “WPF Label Suffix” because I am looking at the Suffix attribute for the XAML Label element. Yours were the third and fourth answers in the list!

    Stephen

    Comment by Stephen Smith | September 23, 2011 | Reply

  2. yes, that blog post was popular. It was given as an answer in the WPF MSDN forum

    Comment by evpo | September 23, 2011 | Reply


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

%d bloggers like this: