简体   繁体   English

通过触发器更改文本块中文本的颜色

[英]Changing colour of text in a textblock via a trigger

Here is my Xaml 这是我的Xaml

 <Window.Resources>
    <sampleData:MainWindow x:Key="DataSource"/>
    <DataTemplate x:Key="bobReferencer">                      
        <TextBlock Text="{Binding Name}" >
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding HasErrors}" Value="true">
                          //what goes in here?
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>                                            
    </DataTemplate>    
</Window.Resources>

Codebehind (the one xaml references) 代码隐藏(一个XAML参考)

public class bob
{

    public string Name
    {
        get;
        set;
    }

    public bool HasErrors
    {
        get;
        set;
    }
 }

Basically what i want to do is if the HasErrors is true then i want the Name to appear in Red via the trigger. 基本上我想做的是,如果HasErrors为true,那么我希望名称通过触发器以红色显示。 But my xaml is not properly formed. 但是我的xaml格式不正确。 Any suggestions on this? 有什么建议吗? I also looked into this link but didn't help much. 我也研究了此链接,但并没有太大帮助。
How can I change the Foreground color of a TextBlock with a Trigger? 如何使用触发器更改TextBlock的前景颜色?

You were almost there.. 你快到了..

        <Style TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding HasErrors}" Value="true">
                  <Setter Property="Foreground" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

在DataTrigger中添加设置器

 <Setter Property="Foreground" Value="Red"/>

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

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