Why there is only "\r" in the binding value of my multiple line TextBox?

322 views Asked by At

I have a TextBox defined as below:

  <TextBox x:Name="TextBoxName" 
    TextWrapping="Wrap"
    AcceptsReturn="True"
    BorderThickness="0" 
    PlaceholderText="{Binding PlaceHolderText}"
    Text="{Binding TestValue, Mode=TwoWay}" />

When I type test1, then type Enter key, and then type test2 in this multiple lines text box, I check the value of TestValue and I see test1\rtest2. I am confused, why the return is displayed as \r only here. In windows isn't it be \r\n?

If I want to write test1\rtest2 into xml, can I replace \r with \n which is unix style? I tried to use this but it doesn't work.

    XmlWriterSettings xws = new XmlWriterSettings();
    xws.NewLineChars = "\n";
    xws.NewLineHandling = NewLineHandling.Replace;
1

There are 1 answers

0
Faywang - MSFT On BEST ANSWER

From this post, it mentions this line breaks(only \r) are by design. So if you want to replace "\r" with "\n" in xml, as a workaround, you could first replace the "\r" in textbox's Text and then write it into xml.

string text;
if (TextBoxName.Text.Contains("\r")) { 
    text = TextBoxName.Text.Replace("\r", "\n");
}

//use text to do something.

Or use Environment.NewLine method to get the newline string defined for this environment.

if (TextBoxName.Text.Contains("\r"))
{
    text = TextBoxName.Text.Replace("\r", Environment.NewLine);
}