问题
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. 
 Here is 
 some 
 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