views:

40

answers:

1

My applications store all localized text in a string resource dictionary as suggested here http://msdn.microsoft.com/en-us/library/bb295251(VS.85).aspx

        <ResourceDictionary 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:system="clr-namespace:System;assembly=mscorlib">

  <!-- String resource that can be localized -->
  <system:String x:Key="localizedMessage">en-US Message</system:String>

</ResourceDictionary>

My question is, how can I add a new line to a string resource and have it render correctly when it's used in a TextBlock.

Doing this inline works:

<TextBlock Name="test" Text="Line 1&#13;Line 2"> </TextBlock>

However, using &#13; in a string resource does not work. What's the best way to add a new line to a string resource?

+2  A: 

UPDATE: updated answer - better option

The XAML parser normalized whitespace according to the following rules.

http://msdn.microsoft.com/en-us/library/cc189036(VS.95).aspx#whitespace

To instruct your sys:String to preserve whitespace, apply xml:space="preserved to it:

<sys:String x:Key="MyLocalizedString" xml:space="preserve">this&#13;&#10;is&#13;&#10;my&#13;&#10;resource</sys:String>
Adam Sills
The localized string option does work, but the binding expression is a bit ugly.
Brent
Yep. But that's what you're stuck with - it would be nice if you could provide an explicit or implicit conversion operator on the LocalizedString type and have the parser pick it up, but it doesn't. I can show a much more complicated option involving an attached property, but I'm not sure that's any better in the long run.
Adam Sills
Updated the answer above with the attached property option.
Adam Sills
Note: updated answer to be more XAML specific (with actual non workaround solution)
Adam Sills