获取多行 wpf 文本块中的所有文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16133200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:38:44  来源:igfitidea点击:

Get all text in multiline wpf textblock

wpf

提问by mich

I have Multiline TextBlock, I want to get all it's lines by code Can someone help me?

我有 Multiline TextBlock,我想通过代码获取所有行 有人可以帮助我吗?

The TextBlock:

文本块:

<TextBlock Name="tb" TextWrapping="Wrap" >
                              Name:_____________
                                <LineBreak/>
                                 Mark:____________
          </TextBlock>

In C#:

在 C# 中:

text = ((TextBlock)tb).Text;

But I got only the first line.

但我只得到了第一行。

Thanks!

谢谢!

回答by Johan Larsson

You can try this:

你可以试试这个:

StringBuilder s = new StringBuilder();
foreach (var line in tb.Inlines)
{
    if (line is LineBreak)
        s.AppendLine();
    else if (line is Run)
        s.Append(((Run) line).Text);
}
var text = s.ToString();

Found it here

在这里找到

回答by kamlendra

Here it show 3 possible ways of getting this done. Please use which suits your requirement.

这里展示了 3 种可能的方法来完成这项工作。请使用适合您的要求。

1.<LineBreak />
2.TextWrapping="Wrap"
3.TextTrimming="CharacterEllipsis"

here

这里

回答by Dragos Stoica

If you want to display on multiple lines you can use :

如果要显示在多行上,可以使用:

<TextBlock Name="myText" Text="I go &#x0a; Home " >

and sure, you can get all lines by parsing the string.

当然,您可以通过解析字符串来获取所有行。