简体   繁体   English

使用Validation.HasError触发属性WPF时仅显示错误的样式

[英]Only false style displaying when using Validation.HasError trigger property WPF

I have a custom style for a text box that I only want to allow numerical values to be input. 我有一个自定义样式的文本框,我只想允许输入数值。

I have created a custom ValidationRule to check for this. 我创建了一个自定义的ValidationRule进行检查。

If the input is valid I want to display a green border and if not I want to display a red border. 如果输入有效,我想显示绿色边框,否则,我想显示红色边框。 The red border is displayed correctly when there is an error however the green is not displayed correctly when the input is correct, it just displays textbox defaults. 出现错误时,红色边框会正确显示,但是输入正确时,绿色边框不会正确显示,它只会显示文本框默认值。

The ValidateResult method in my custom ValidationRule is: 我的自定义ValidationRule中的ValidateResult方法是:

 public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {            
        string input = (value ?? String.Empty).ToString();
        double val;
        if (double.TryParse(input, out val))
        {
            return new ValidationResult(true, OkMessage);
        }
        else return new ValidationResult(false, ErrorMessage);
    }

The Style and Triggers are defined: 定义样式和触发器:

<Style TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="Validation.ErrorTemplate">
                    <Setter.Value>
                        <ControlTemplate>                                
                                <Border BorderBrush="Red" BorderThickness="1"/>
                            </DockPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="Validation.HasError" Value="False">
                <Setter Property="Validation.ErrorTemplate">
                    <Setter.Value>
                        <ControlTemplate>                                
                                <Border BorderBrush="Green" BorderThickness="1"/>
                            </DockPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

How can I get the border to display green when the input is correctly validated? 输入正确验证后,如何使边框显示为绿色?

I believe this is because Validation.HasError is cleared when there is no error, not set to false as your trigger requires. 我相信这是因为没有错误时会清除 Validation.HasError ,而不是触发器需要将其设置为false Why not just include the green border in your template? 为什么不在模板中包括绿色边框? The HasError trigger will change it to red, but at all other times it should be green so best just to place that inline rather than a trigger. HasError触发器会将其更改为红色,但在其他所有时间都应为绿色,因此最好只是将其内联而不是触发器。

The reason is you are setting ErrorTemplate which will he shown when there is an error... 原因是您正在设置ErrorTemplate,当出现错误时,它将显示...

you are changing error template color to green but error template will not be visible when there is no error... 您将错误模板的颜色更改为绿色,但没有错误时将看不到错误模板...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM