Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

c# - programmatic textblock entry with linebreaks

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

helpBlock.Text = "Here is some text. 
 Here is 
 some 
 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...