What is wrong with this TextBox style?

Why does this simple style not work for TextBox? I expect the background / foreground color to change as the text changes between "0" and "1" ...

<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
           <Setter Property="Background" Value="Gray"/>

            <Style.Triggers>               
                <!-- If the Textbox holds a value of 1, then update the foreground/background -->
                <DataTrigger Binding="{Binding Path=Text}" Value="1">
                    <Setter Property="Foreground" Value="Black"/>
                    <Setter Property="Background" Value="White"/>
                </DataTrigger>

                <!-- If the Textbox holds a value of 0, then update the foreground/background -->
                <DataTrigger Binding="{Binding Path=Text}" Value="0">
                    <Setter Property="Foreground" Value="White"/>
                    <Setter Property="Background" Value="Black"/>
                </DataTrigger>
            </Style.Triggers>
 </Style>
+3
source share
1 answer

You are using a DataTrigger, but a trigger would be better in this case:

    <Trigger Property="Text" Value="1">
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="Background" Value="White"/>
    </Trigger>
+3
source

Source: https://habr.com/ru/post/1765323/


All Articles