简体   繁体   中英

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

I have an xml file like this:

<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:

   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++?

UPDATE:
- Text is read from Xml by loading in a XDocument and reading the value into the TextBlock using Linq
- Yes, the CRLF characters are visible when seen in Notepad++
- TextBlock has TextWrapping enabled with "Wrap" - Sample xml: http://sdrv.ms/1gGbeE3

just add the property 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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