更改标签字符串中单个字母的颜色?

Cod*_*e92 6 vb.net wpf label colors

我在WPF 4和VB.net中有一个项目.我需要在标签中的单词中更改单个字母的颜色(标签的内容会发生相当大的变化).我真的不确定这是否可能,但如果是的话,我会很感激帮助找出方法.TY!

bij*_*iju 13

标签是一种内容控件,因此标签内允许任何类型的内容.您可以通过类似的方式轻松完成您的要求

<Label>
    <StackPanel Orientation="Horizontal">
        <TextBlock Foreground="Red" Text="T"/>
        <TextBlock Text="ext"/>
    </StackPanel>
</Label>
Run Code Online (Sandbox Code Playgroud)


H.B*_*.B. 13

更简洁的方法是使用TextBlock的flow-content-capabilites:

<Label>
    <TextBlock>
        <Run Text="L" Foreground="Green"/>
        <Run Text="orem Ipsum"/>
    </TextBlock>
</Label>
Run Code Online (Sandbox Code Playgroud)

如果需要,这会限制绑定一点.


H.B*_*.B. 8

到目前为止我发现的最干净的方法是使用TextEffect:

<Label>
    <TextBlock Text="Search">
        <TextBlock.TextEffects>
            <TextEffect PositionStart="0" PositionCount="1" Foreground="Red"/>
        </TextBlock.TextEffects>
    </TextBlock>
</Label>
Run Code Online (Sandbox Code Playgroud)

这会使"S"红色.如果需要动态,您当然可以绑定任何涉及的属性.