Add carriage return to string resource in WPF

Viewed 14332

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?

3 Answers

When you are entering a string resource in the resource view you can add CR/LF by hitting Shift + Enter. It will add the newline and you'll be able to see it. Retrieving the resource string and setting it as text on the text block will then yield the desired effect of reproducing the newline (or multiple newlines). In my case I wanted to simulate two paragraphs in a single text block. I didn't like the other approaches because it required the translation into another language to deal with two strings. I wanted to treat this as a single string resource so that the translator had the full context of what they were translating.

Related