2014-08-28 5 views
3

내 WPF 응용 프로그램에서 나는 중앙 집중식 사전 리소스에서 문자열을 참조하고 있습니다. 어떻게이 문자열에 줄 바꿈을 넣을 수 있습니까?문자열 리소스의 WPF 줄 바꿈

나는 "line1\nline2", "line1\\nline2" and "line1
line2"을 시도했지만 아무도 작동하지 않습니다.

나는이 문자열 ({0}, ...)에 토큰을 포함하고 나중에 런타임에 string.format (resource, args)을 사용한다는 것을 언급해야합니다. 당신이 실제로 Inline를 인코딩하는 경우 사용할 수있는, 그러나,

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

참고 :

+0

line breaksa를 넣어야하는 경우 2 개의 다른 문자열로 문자열을 분할하지 않으시겠습니까? – Juan

+0

'xml : space = "preserve"를 ResourceDictionary 나 단일 리소스에 추가해야합니다. – marbel82

답변

8

해결 방법 : Visual Studio의 사전 리소스 창에 shift + enter가 작동하는 것 같습니다.

2

는 리터럴 진수를 시도

<LineBreak /> 

예 :

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

에 시도 리소스에 xml:space="preserve"을 추가하고 & # 13

을 사용하십시오.
<sys:String x:Key="MyString" xml:space="preserve">line1&#13line2</sys:String> 
+1

xml : space = "preserve"는 테스트에서 깨집니다. 세미콜론도 잊어 버렸습니다. – codekaizen

1

아무 것도 해결되지 않으면 변환기를 사용하는 것이 좋습니다.

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(); 
    } 
}