开发者

How to specify a trigger for an empty TextBox in WPF?

开发者 https://www.devze.com 2023-01-14 11:58 出处:网络
I have a style applied to a TextBox. The style contains two triggers - both of which check the property Validation.HasError. A different image is displayed on the background of the TextBox indicating

I have a style applied to a TextBox. The style contains two triggers - both of which check the property Validation.HasError. A different image is displayed on the background of the TextBox indicating if the data is valid or not.

I'm trying to change the style so that when the TextBox is empty, no image is displayed in the background. I was hoping to find another property to use in the trigger that would allow me to check if it is empty or not, but I can't find one.

Am I going about this the wrong way? Here is the style as it is currently:

<Style x:Key="ValidationTextBox" TargetType="TextBox">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="false">
            <Setter Property="TextBox.Background">
                <Setter.Value>
                    <ImageBrush ImageSource="fieldWhite_check.png" AlignmentX="Right" AlignmentY="Top" Stretch="None" />
                </Setter.Value>
            </Setter>
        </Trigger>          
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="TextBox.Background">
                <Setter.Value>
                    <ImageBrush ImageSource="fieldWhite_error.png" AlignmentX="Right" AlignmentY="Top" Stretch="None" />
                </Setter.Value>
            </Setter>
        </Trig开发者_如何学Goger>
    </Style.Triggers>
</Style>


You have multiTrigger, so you can check for the value to be Empty "" or to be null {x:Null} and keep checking for the HasError property http://en.csharp-online.net/WPF_Styles_and_Control_Templates%E2%80%94Multi-Condition_Property_Trigger

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号