Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change TextBlock Background based on the value of Text

I want to change the Background property of a TextBlock if the Text is 00:00.Is there a simple, XAML solution? I've tried creating a DataTrigger but it's not changing the colour

        <Style TargetType="TextBlock" x:Key="textBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding}" Value="00:00">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

<TextBlock Text="{Binding}" Margin="3" Style="{DynamicResource textBlock}"/>
like image 689
Denys Wessels Avatar asked Mar 21 '23 01:03

Denys Wessels


2 Answers

What about a normal Trigger?:

<Style TargetType="TextBlock" x:Key="textBlock">
    <Style.Triggers>
        <Trigger Property="TextBlock.Text" Value="00:00">
            <Setter Property="Background" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>
like image 144
Sheridan Avatar answered Apr 01 '23 11:04

Sheridan


I think your binding in your datatrigger should be

Binding="{Binding RelativeSource={RelativeSource Self},Path=Text}"
like image 22
TylerD87 Avatar answered Apr 01 '23 12:04

TylerD87