WPF前台数据验证(红框)Validation.ErrorTemplate 附加属性

WPF 显示验证错误的默认方式是在控件周围绘制红色边框。通常需要对此方法进行自定义,以通过其他方式来显示错误。而且,默认情况下不会显示与验证错误关联的错误消息。常见的要求是仅当存在验证错误时才在工具提示中显示错误消息。通过将 Styles 和一组与验证关联的附加属性进行组合,可以相当轻松地自定义验证错误显示。

WPF前台数据验证(红框)Validation.ErrorTemplate 附加属性_第1张图片

前台xaml:


    
    
        
        
        
    
    

    
    
    

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication11111
{
    /// 
    /// UserControl2.xaml 的交互逻辑
    /// 
    public partial class UserControl2 : UserControl
    {
        private UserInfo _UserInfo;
        public UserControl2()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(UserControl2_Loaded);
        }

        void UserControl2_Loaded(object sender, RoutedEventArgs e)
        {
            _UserInfo = new UserInfo();
            this.DataContext = _UserInfo;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            //txtName.Visibility = Visibility.Collapsed;
            UserControl1 _UserControl1 = new UserControl1();
            grid.Children.Add(_UserControl1);
            string _name = _UserInfo.Name;
            string _pass = _UserInfo.Pass;
        }
    }
}

实体类:

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

namespace WpfApplication11111
{
    public class UserInfo : ValidationUtility, INotifyPropertyChanged
    {
        #region 数据更新通知

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        private string _Name;

        [Required(ErrorMessage = "[登录名]内容不能为空!")]
        [StringLength(255, ErrorMessage = "[登录名]内容最大允许255个字符!")]
        [RegularExpression("^[A-Za-z0-9]+$", ErrorMessage = "[登录名]格式不正确!")]
        /// 
        /// 
        /// 
        public string Name
        {
            get { return _Name; }
            set
            {
                //Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Name" });
                //if (string.IsNullOrEmpty(value))
                //{
                //    throw new Exception("用户名不能为空.");
                //}
                _Name = value;
                NotifyPropertyChange("Name");
            }
        }

        private string _Pass;
        [Required(ErrorMessage = "[密码]内容不能为空!")]
        [StringLength(255, ErrorMessage = "[密码]内容最大允许255个字符!")]
        [RegularExpression("^[A-Za-z0-9]+$", ErrorMessage = "[密码]格式不正确!")]
        /// 
        /// 
        /// 
        public string Pass
        {
            get { return _Pass; }
            set
            {
                //if (string.IsNullOrEmpty(value))
                //{
                //    throw new Exception("密码不能为空.");
                //}
                _Pass = value;
                NotifyPropertyChange("Pass");
            }
        }

    }
}

ValidationUtility.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.ComponentModel.DataAnnotations;

namespace WpfApplication11111
{
    public class ValidationUtility : IDataErrorInfo
    {
        public string Error
        {
            get { return _error; }
        }

        public string _error;

        public string this[string columnName]
        {
            get
            {
                Type tp = this.GetType();
                PropertyInfo pi = tp.GetProperty(columnName);
                var value = pi.GetValue(this, null);
                object[] Attributes = pi.GetCustomAttributes(false);
                if (Attributes != null && Attributes.Length > 0)
                {
                    foreach (object attribute in Attributes)
                    {
                        if (attribute is ValidationAttribute)
                        {
                            ValidationAttribute vAttribute = attribute as ValidationAttribute;
                            if (!vAttribute.IsValid(value))
                            {
                                _error = vAttribute.ErrorMessage;
                                return _error;
                            }
                        }
                    }
                }
                return null;
            }
        }
    }
}

追加PasswordBox验证



代码下载地址:

http://download.csdn.net/detail/hwt0101/5070730

相关地址:

http://msdn.microsoft.com/zh-cn/magazine/ff714593.aspx

http://msdn.microsoft.com/zh-cn/library/system.windows.controls.validation.errortemplate(v=vs.90).aspx
http://msdn.microsoft.com/en-us/library/ms752068(v=vs.100).aspx
http://wpf.codeplex.com/releases/view/14962

你可能感兴趣的:(C#,Silverlight)