简体   繁体   English

如何在WPF TextBlock中使用CRLF显示多行xml内容?

[英]How to show multi-line xml content with CRLF in WPF TextBlock?

I have an xml file like this: 我有一个像这样的xml文件:

<Items>
 <Item att1="val1" att2="val2" att3="
   att3 line 1 is long
   att3 line 2 is longer
   att3 line 3 is longest"/>
 ...
</Items>

The WPF TextBlock should show the value of att3 as follows: WPF TextBlock应该显示att3的值,如下所示:

   att3 line 1 is long
   att3 line 2 is longer
   att3 line 3 is longest

Instead I see the following: 相反,我看到以下内容:

att3 line 1 is long att3 line 2 is 
longer att3 line 3 is longest

How do I get the WPF TextBlock to recognise CRLF characters which I can see when I open the file in NotePad++? 我如何获得WPF TextBlock来识别在NotePad ++中打开文件时可以看到的CRLF字符?

UPDATE: 更新:
- Text is read from Xml by loading in a XDocument and reading the value into the TextBlock using Linq -通过加载XDocument并使用Linq将值读取到TextBlock中,从Xml中读取文本
- Yes, the CRLF characters are visible when seen in Notepad++ -是的,在记事本++中可以看到CRLF字符
- TextBlock has TextWrapping enabled with "Wrap" - Sample xml: http://sdrv.ms/1gGbeE3 -TextBlock启用了带有“换行”的TextWrapping-示例xml: http ://sdrv.ms/1gGbeE3

just add the property TextWrapping="Wrap" 只需添加属性TextWrapping="Wrap"

<TextBlock  Name="textBlock" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>

I have tried this in code behind and works fine so you should be sure that you have \\n in your string 我已经在后面的代码中尝试过并且可以正常工作,因此您应该确保您的字符串中有\\ n

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        string st = " att3 line 1 is long \n  att3 line 2 is longer \n   att3 line 3 is longest";
        textBlock.Text = st;  
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM