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

时间:2013-12-17 22:08:37

标签: c# xml wpf newline textblock

我有一个像这样的xml文件:

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

WPF TextBlock应该显示att3的值,如下所示:

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

相反,我看到以下内容:

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

如何让WPF TextBlock识别我在NotePad ++中打开文件时可以看到的CRLF字符?

更新
- 通过在XDocument中加载并使用Linq
将值读入TextBlock,从Xml读取文本 - 是的,在Notepad ++中看到的CRLF字符是可见的 - TextBlock启用了TextWrapping“Wrap” - 示例xml:http://sdrv.ms/1gGbeE3

1 个答案:

答案 0 :(得分:0)

只需添加属性TextWrapping="Wrap"

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

我已经在代码中尝试了这个并且工作正常,所以你应该确保你的字符串中有\ 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;  
    }
}