allquixotic
allquixotic

Reputation: 1559

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

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).

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.

Upvotes: 7

Views: 18039

Answers (3)

IQ.feature
IQ.feature

Reputation: 648

You can store string values in "Resources.resw", and get them in code directly. In the VS resource editor itself not possible to add empty lines to string values. To do this, just edit value in any text editor, add empty lines how many you need, and "Copy -> Paste" to Value column of Visual Studio's resource editor.

For example: value of "Farewell" with 2 line breaks: Goodbye\n\nSave changes? - will look like this in text editor (and as it is, need to be copied to VS):

Goodbye

Save changes?

enter image description here

To get string value of the resource in C# code, call 1 of the following lines (UWP or WinUI3), depending on your application.

UWP code:

Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView().GetString("Farewell");

WinUI3 code:

new ResourceLoader().GetString("Farewell");

Also can be simplified [add to App.xam.cs]:

public static AppResources _AppResources { get; } = new AppResources();
public static string GetString(string resourceName) => _AppResources.GetStringResource(resourceName);

And add to XAML file:

<TextBlock Text="{x:Bind local:App.GetString('Farewell')}" />

Upvotes: 1

okieh
okieh

Reputation: 627

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.

Upvotes: 1

metacubed
metacubed

Reputation: 7271

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?"));

Upvotes: 21

Related Questions