简体   繁体   English

在标签内获取/设置TextBlock XML文本值

[英]Get/Set TextBlock XML Text Value within it's tags

I can get and set the Text property of a TextBlock but i don't know to get the text corresponding the the one inside XAML: 我可以获取并设置TextBlockText属性,但是我不知道要获取与XAML内部对应的文本:

<TextBlock x:Name="myTextBlock">This is what i want to get/set</TextBlock>

I want to add html special characters/codes that's why the "text" attribute doesn't help, for example: 我想添加html特殊字符/代码,这就是为什么“ text”属性无济于事的原因,例如:

<TextBlock x:Name="myTextBlock">&#160;&#160;Whatever<LineBreak />More Text</TextBlock>

Which works perfectly inside TextBlock tags but not when setting the property 它在TextBlock标记内完美运行,但在设置属性时无效

myTextBlock.Text = "whatever..."

which breaks the content and instead shows: 它破坏了内容,而是显示:

&#160;&#160;Whatever<LineBreak />More Text

First of all, the Text attribute sets the Text property, the element content corresponds to the Inlines collection, which can be mixed content. 首先, Text属性设置Text属性,元素内容对应于Inlines集合,可以将其混合在一起。 Text is essentially shorthand for a single Run . Text本质上是单个Run简写。

If you want to set those in code you can do that, but the encoding of Unicode works differently. 如果要在代码中设置这些代码,则可以执行此操作,但是Unicode的编码方式不同。 If you want line breaks as in XAML you can either build the same structure using Runs for strings and the LineBreak inlines or encode the line-break directly. 如果要像XAML中那样换行,则可以使用Runs for string和LineBreak联来构建相同的结构,也可以直接对换行进行编码。

In a normal C# string you can use \\uXXXX syntax. 在普通的C#字符串中,您可以使用\\uXXXX语法。 For 💤 it would be "\?\?" . 对于💤 ,它将是"\?\?"

If you have complex text with styling you will need to read the Inlines to get all the information from the TextBlock . 如果您具有样式复杂的文本,则需要阅读Inlines以从TextBlock获取所有信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM