programmatic textblock entry with linebreaks
Asked Answered
A

5

14

How do I programmatically add text with line breaks to a textblock?

If I insert text like this:

helpBlock.Text = "Here is some text. <LineBreak/> Here is <LineBreak/> some <LineBreak/> more.";

Then the linebreaks get interpreted as part of the string literal. I want it to be more like what would happen if I had it in the XAML.

I can't seem to do it the WPF way either:

helpBlock.Inlines.Add("Here is some content.");

Since the Add() method wants to accept objects of type "inline".

I can't create an Inline object and pass it as a parameter because it is "inaccessible due to its protection level:

helpBlock.Inlines.Add(new Windows.UI.Xaml.Documents.Inline("More text"));

I don't see a way to programmatically add runs.

I can find a ton of WPF examples of this, but nothing for WinRT.

I've also turned up a lot of XAML examples, but nothing from C#.

Arguelles answered 23/3, 2013 at 1:34 Comment(0)
A
19

You could just pass in newline \n instead of <LineBreak/>

helpBlock.Text = "Here is some text. \n Here is \n some \n more.";

Or in Xaml you would use the Hex value of newline

 <TextBlock Text="Here is some text. &#x0a; Here is &#x0a; some &#x0a; more."/>

Both results:

enter image description here

Alby answered 23/3, 2013 at 2:36 Comment(2)
I wonder if there is an advantage to doing \r\n instead of \n.Arguelles
I marked this the best answer because it is so simple and will probably simplify things for loading from an XML file.Arguelles
H
9

Use Enviroment.NewLine

testText.Text = "Testing 123" + Environment.NewLine + "Testing ABC";

StringBuilder builder = new StringBuilder();
builder.Append(Environment.NewLine);
builder.Append("Test Text");
builder.Append(Environment.NewLine);
builder.Append("Test 2 Text");
testText.Text += builder.ToString();
Hyacinthie answered 23/3, 2013 at 1:43 Comment(0)
G
1

You could convert \n to <LineBreak/> programmatically.

    string text = "This is a line.\nThis is another line.";
    IList<string> lines = text.Split(new string[] { @"\n" }, StringSplitOptions.None);

    TextBlock tb = new TextBlock();
    foreach (string line in lines)
    {
        tb.Inlines.Add(line);
        tb.Inlines.Add(new LineBreak());
    }
Goingson answered 3/9, 2015 at 13:40 Comment(0)
P
1

Solution:

I would use "\n" instead of linebreaks. Best way would be use it on this way:

Resources.resx file:

myTextline: "Here is some text. \n Here is \n some \n more."

In yours Class:

helpBlock.Text = Resources.myTextline;

This will looks like:

enter image description here

Other solution would be to build your string here with Environment.NewLine.

StringBuilder builder = new StringBuilder();
builder.Append(Environment.NewLine);
builder.Append(Resources.line1);
builder.Append(Environment.NewLine);
builder.Append(Resources.line2);
helpBlock.Text += builder.ToString();

Or use here "\n"

StringBuilder builder = new StringBuilder();
builder.Append("\n");
builder.Append(Resources.line1);
builder.Append("\n");
builder.Append(Resources.line2);
helpBlock.Text += builder.ToString();
Prowl answered 3/9, 2015 at 13:52 Comment(0)
M
0

I have one component XAML

<TextBlock x:Name="textLog" TextWrapping="Wrap" Background="#FFDEDEDE"/>

Then I pass the string + Environment.NewLine;

Example:

textLog.Inlines.Add("Inicio do processamento " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + Environment.NewLine);
textLog.Inlines.Add("-----------------------------------" + Environment.NewLine);

The result is

Inicio do processamento 19/08/2019 20:31:13
-----------------------------------

Marjorie answered 19/8, 2019 at 23:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.