programmatic textblock entry with linebreaks

孤街浪徒 提交于 2020-01-14 07:34:12

问题


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


回答1:


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:




回答2:


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();



回答3:


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



回答4:


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:

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();



回答5:


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



来源:https://stackoverflow.com/questions/15582398/programmatic-textblock-entry-with-linebreaks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!