这个TextBox样式有什么问题?

时间:2010-09-17 16:05:50

标签: wpf wpf-controls

为什么这个简单的Style不能用于TextBox?当我在“0”和“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>

1 个答案:

答案 0 :(得分:3)

您使用DataTrigger但在这种情况下更好的是触发器:

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