using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace FWindSoft.Wpf
{
/*
DataErrorValidationRule,ExceptionValidationRule
*/
public class TValidationRule: ValidationRule
{
///
/// 错误信息
///
public string ErrorMessage { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (value == null)
return new ValidationResult(false, ErrorMessage??"不能为空值!");
if (string.IsNullOrWhiteSpace(value.ToString()))
return new ValidationResult(false, ErrorMessage ?? "不能为空字符串!");
return new ValidationResult(true, null);
}
}
public class RequireValidationRule : TValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (value == null)
return new ValidationResult(false, ErrorMessage ?? "不能为空值!");
if (string.IsNullOrWhiteSpace(value.ToString()))
return new ValidationResult(false, ErrorMessage ?? "不能为空字符串!");
return new ValidationResult(true, null);
}
}
#region 正则表达式判定
///
/// 这则表达式验证基类
///
public class RegexValidationRule : TValidationRule
{
protected string m_Match;
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (value==null||string.IsNullOrWhiteSpace(value.ToString()))
return new ValidationResult(true, null);
if (m_Match == null)
return new ValidationResult(true, null);
if (!string.IsNullOrWhiteSpace(value.ToString()))
{
Regex tempMatch = new Regex(m_Match);
if (!tempMatch.IsMatch(value.ToString()))
{
return new ValidationResult(false, ErrorMessage ?? "信息错误");
}
}
return new ValidationResult(true, null);
}
}
public class EMailValidationRule : RegexValidationRule
{
public EMailValidationRule()
{
m_Match = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
}
}
public class FaxValidationRule : RegexValidationRule
{
public FaxValidationRule()
{
m_Match = @"^(\d{3,4}-?)?\d{7,8}$";
}
}
public class PhoneValidationRule : RegexValidationRule
{
public PhoneValidationRule()
{
m_Match = @"^(\d{3,4}-?)?\d{7,8}$";
}
}
public class CustomRegexValidationRule : RegexValidationRule
{
public CustomRegexValidationRule()
{
}
public string Match
{
get { return this.m_Match; }
set { this.Match = value; }
}
}
#endregion
#region 使用委托判定
public class MethodValidationRule : TValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
//if (value == null)
// return new ValidationResult(false, ErrorMessage ?? "不能为空值!");
if(Method!=null)
return Method?.Invoke(value);
return new ValidationResult(true, null);
}
public Func