Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert a newline into a TextBlock without XAML?

Tags:

c#

wpf

I have created a WPF TextBlock inside a Label in code (XAML is not possible in my case) as follows:

Label l = new Label();
TextBlock tb = new TextBlock();
l.Content = tb;

I then come to a situation where I need to set the .Text property of TextBlock containing a new line, such as:

tb.Text = "Hello\nWould you please just work?";

I have tried numerous encodings (HTML encoding, ASCII encoding, etc.) of various newline combinations (carriage return, linefeed, carriage return plus linefeed, linefeed plus carriage return, double linefeed, double carriage return, etc... ad nauseum).

  • Answers should contain absolutely no XAML.
  • Answers should assume that the original objects were created using C# code and not XAML.
  • Answers should not refer to binding of WPF properties. No binding is being used. The "Text" property of the "TextBlock" object is being set. The newline must be inserted there.

If this is impossible, please let me know how I can programmatically add newlines by dynamically replacing each newline in an arbitrary input String into a LineBreak object (or whatever is needed to get this working). The input String will have arbitrary (readable text) contents that I am unable to anticipate in advance because the text itself is dynamic (user-defined); the source strings will have the linefeed character (aka LF, aka \n) but I can easily replace that with whatever is needed.

Also, if it is easier to do this with a Label directly rather than a TextBlock in a Label, that is good too -- I can use that. I just need a control with automatic plain text line wrapping.

like image 488
allquixotic Avatar asked Jun 10 '14 00:06

allquixotic


People also ask

How to add new line in TextBlock wpf?

Adding Line Breaks You can do this with a LineBreak inline, added as an XAML element within the text. A new line will be started at the position of this element. If you have configured the text to be justified, the final line before the line break will not be expanded to fill the available width.

How do you insert a line break in XAML?

XAML attributes of type String may contain any special characters, as long as those are referenced as hex-codes. For example, a simple line break ( \n ) would be represented as 
 , for \r\n you'd use 
 and so on.

How do you add a line in WPF?

To draw a line, create a Line element. Use its X1 and Y1 properties to set its start point; and use its X2 and Y2 properties to set its end point. Finally, set its Stroke and StrokeThickness because a line without a stroke is invisible. Setting the Fill element for a line has no effect, because a line has no interior.

Is TextBlock editable?

TextBlocks can be edited by users using the TextEditingTool. The HTMLInfo that a given TextBlock uses as its text editor can be customized by setting the textEditor property.


2 Answers

You have a few choices:

Use the Environment.NewLine property:

TextBlock tb = new TextBlock();
tb.Text = "Hello" + Environment.NewLine + "Would you please just work?";

Or, manually add Runs and LineBreaks to the TextBlock:

TextBlock tb = new TextBlock();
tb.Inlines.Add(new Run("Hello"));
tb.Inlines.Add(new LineBreak());
tb.Inlines.Add(new Run("Would you please just work?"));
like image 177
metacubed Avatar answered Sep 28 '22 07:09

metacubed


Just another small note. I had a similar problem and took the string from a resource file. What I noticed is that .NET delivered the string "\r\n" as "\\r\\n". When I corrected that by replacing the double-backslashes with normal backslashes everything worked as expected.

like image 24
okieh Avatar answered Sep 28 '22 07:09

okieh