简体   繁体   中英

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

Result of binding should be: <TextBlock>get <Span Foreground="#00FF00">35</Span> mana</TextBlock>

在此处输入图像描述

Please help with solving the problem.

TextBlock can't interpret HTML. You can archive it by WebBrowser control. I think that under this link you can see possible solution: Displaying html from string in WPF WebBrowser control

for TextBlock you can declare Inlines (Span equivalent is 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 ManaValue, Mode=OneWay}" Foreground="#00FF00"/>

There are also examples how to Bind all InLines: see this answer by B. Tossings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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