简体   繁体   English

如何通过绑定将带有标记的字符串放入TextBlock? (WPF)

[英]How to put string with markup into TextBlock by binding? (WPF)

I have ability objects, each has a property with a string like get <Span Foreground="#00FF00">35</Span> mana .我有能力对象,每个对象都有一个带有字符串的属性,例如get <Span Foreground="#00FF00">35</Span> mana In the application, the text should be displayed with a green number.在应用程序中,文本应显示为绿色数字。

But with Binding <TextBlock Text="{Binding SkillDescription}" /> "Span" is not applied, but is displayed as text.但是使用 Binding <TextBlock Text="{Binding SkillDescription}" /> “Span”不适用,而是显示为文本。

Result of binding should be: <TextBlock>get <Span Foreground="#00FF00">35</Span> mana</TextBlock>绑定的结果应该是: <TextBlock>get <Span Foreground="#00FF00">35</Span> mana</TextBlock>

在此处输入图像描述

Please help with solving the problem.请帮助解决问题。

TextBlock can't interpret HTML. TextBlock 无法解释 HTML。 You can archive it by WebBrowser control.您可以通过 WebBrowser 控件对其进行归档。 I think that under this link you can see possible solution: Displaying html from string in WPF WebBrowser control我认为在此链接下您可以看到可能的解决方案: Displaying html from string in WPF WebBrowser control

for TextBlock you can declare Inlines (Span equivalent is Run ):对于 TextBlock 您可以声明 Inlines (Span 等效项是Run ):

<TextBlock>
    <Run Text="get"/>
    <Run Text="35" Foreground="#00FF00"/>
    <Run Text="mana"/>
</TextBlock>

Run.Text supports Binding, so it can be, for example: Run.Text 支持 Binding,所以它可以是,例如:

<Run Text="{Binding ManaValue, Mode=OneWay}" Foreground="#00FF00"/>

There are also examples how to Bind all InLines: see this answer by B. Tossings还有一些示例如何绑定所有内联:请参阅B. Tossings这个答案

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

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