WPF 数据验证

WPF 数据验证

效果:

文件结构:

添加如下引用:

NotifyBase类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp1.Model
{
    public class NotifyBase : INotifyPropertyChanged, IDataErrorInfo
    {
        public string this[string columnName]
        {
            get
            {
                var errors = new List<ValidationResult>();
                Validator.TryValidateProperty(this.GetType().GetProperty(columnName).GetValue(this, null), new ValidationContext(this)
                {
                    MemberName = columnName
                }, errors);

                if (errors.Count > 0)
                {
                    return string.Join(Environment.NewLine, errors.Select(e => e.ErrorMessage).ToArray());
                }
                return "";
            }
        }

        public string Error => null;

        public event PropertyChangedEventHandler PropertyChanged;

        public void Notify([CallerMemberName] string propName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
        }
    }
}

DataErrorInfoModel类:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp1.Model
{
    public class DataErrorInfoModel : NotifyBase
    {
        private string _userName;
        [Required(ErrorMessage = "用户名不能为空")]
        [StringLength(100, MinimumLength = 2, ErrorMessage = "最小长度为2")]
        public string UserName
        {
            get { return _userName; }
            set
            {
                _userName = value;
                this.Notify();
            }
        }

        private string _email;
        [Required(ErrorMessage = "邮箱名不能为空")]
        [StringLength(100, MinimumLength = 2, ErrorMessage = "最小长度为2")]
        [RegularExpression("^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$", ErrorMessage = "请填写正确的邮箱地址!")]

        public string Email
        {
            get { return _email; }
            set
            {
                _email = value;
                this.Notify();
            }
        }
    }
}

窗体xaml代码:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFABAdB3"/>
        <SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FF7EB4EA"/>
        <SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FF569DE5"/>

        <ControlTemplate x:Key="ExceptionTextBoxTemplate">
            <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" CornerRadius="5">
                <Grid>
                    <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                    <Border Width="16" Height="16" CornerRadius="8" Margin="5" Background="Red" VerticalAlignment="Center" HorizontalAlignment="Right" Visibility="Collapsed" Name="validation" ToolTip="{Binding (Validation.Errors)[0].ErrorContent,RelativeSource={RelativeSource AncestorType=TextBox, Mode=FindAncestor}}">
                        <TextBlock Text="!" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </Border>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                </Trigger>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.MouseOver.Border}"/>
                </Trigger>
                <Trigger Property="IsKeyboardFocused" Value="true">
                    <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.Focus.Border}"/>
                </Trigger>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="BorderBrush" Value="Red" TargetName="border"/>
                    <Setter Property="Visibility" Value="Visible" TargetName="validation"/>
                    <Setter Property="ToolTip" Value="{Binding (Validation.Errors)[0].ErrorContent,RelativeSource={RelativeSource Self}}"/>
                    <!--<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />-->
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

    </Window.Resources>
    <StackPanel>
        <TextBox Width="200" Height="40" VerticalContentAlignment="Center" Margin="10" Template="{StaticResource ExceptionTextBoxTemplate}" Validation.ErrorTemplate="{StaticResource ExceptionTextBoxTemplate}">
            <TextBox.Text>
                <Binding Path="DModel.UserName" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
                </Binding>
            </TextBox.Text>
        </TextBox>

        <TextBox Width="200" Height="40" VerticalContentAlignment="Center" Margin="10" Template="{StaticResource ExceptionTextBoxTemplate}" Validation.ErrorTemplate="{StaticResource ExceptionTextBoxTemplate}">
            <TextBox.Text>
                <Binding Path="DModel.Email" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
                </Binding>
            </TextBox.Text>
        </TextBox>
    </StackPanel>
</Window>

后台代码:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = new DataErrorInfoViewModel();
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值