WPF Line breaks in string resources

Try the numeric character reference approach:

<sys:String>line1&#13;line2</sys:String>

Note, however, that if you are actually encoding an Inline you can use:

<LineBreak />

E.g:

<TextBlock>
    <TextBlock.Text>
         line1 <LineBreak /> line2
    </TextBlock.Text>
</TextBlock>

Try to add xml:space="preserve" to your resource and use &#13;

<sys:String x:Key="MyString" xml:space="preserve">line1&#13;line2</sys:String>

If nothing works out, assured solution would be using a converter.

public class NewLineConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var s = string.Empty;

        if (value.IsNotNull())
        {
            s = value.ToString();

            if (s.Contains("\\r\\n"))
                s = s.Replace("\\r\\n", Environment.NewLine);

            if (s.Contains("\\n"))
                s = s.Replace("\\n", Environment.NewLine);

            if (s.Contains("&#x0a;&#x0d;"))
                s = s.Replace("&#x0a;&#x0d;", Environment.NewLine);

            if (s.Contains("&#x0a;"))
                s = s.Replace("&#x0a;", Environment.NewLine);

            if (s.Contains("&#x0d;"))
                s = s.Replace("&#x0d;", Environment.NewLine);

            if (s.Contains("&#10;&#13;"))
                s = s.Replace("&#10;&#13;", Environment.NewLine);

            if (s.Contains("&#10;"))
                s = s.Replace("&#10;", Environment.NewLine);

            if (s.Contains("&#13;"))
                s = s.Replace("&#13;", Environment.NewLine);

            if (s.Contains("<br />"))
                s = s.Replace("<br />", Environment.NewLine);

            if (s.Contains("<LineBreak />"))
                s = s.Replace("<LineBreak />", Environment.NewLine);
        }

        return s;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Working solution: shift+enter in the dictionary resource window in visual studio seems to work.

Tags:

C#

String

Wpf