Sike12
Sike12

Reputation: 1262

Changing colour of text in a textblock via a trigger

Here is my 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)

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. But my xaml is not properly formed. 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?

Upvotes: 2

Views: 3576

Answers (2)

gleng
gleng

Reputation: 6304

You were almost there..

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

Upvotes: 4

Sandesh
Sandesh

Reputation: 3004

Add a setter inside the DataTrigger

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

Upvotes: 2

Related Questions