小编Pio*_*r L的帖子

WPF TextBox 在验证上设置红色边框

我试图在文本框为空时将其边框设为红色。这是我的 xaml:

     <TextBox  Style="{StaticResource TextBoxEmptyError}" Name="tbFilename" Grid.Column="1" >
         <Binding Path="Text" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
            <Binding.ValidationRules>
                <local:EmptyRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox>
Run Code Online (Sandbox Code Playgroud)

我正在尝试设置的样式:

        <Style x:Key="TextBoxEmptyError" TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="True">
                    <Setter Property="BorderThickness" Value="2" />
                    <Setter Property="BorderBrush" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
Run Code Online (Sandbox Code Playgroud)

空规则:

public class EmptyRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (string.IsNullOrEmpty(value as string))
                return new ValidationResult(false, null);
            else
                return new ValidationResult(true, null);

        }
    }
Run Code Online (Sandbox Code Playgroud)

在调试器中,看起来根本没有使用 Validation 方法。我究竟做错了什么?

c# validation wpf

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

validation ×1

wpf ×1