DataRangeValidationRules.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Controls;
  6. namespace Muchinfo.MTPClient.Infrastructure.ValidationRules
  7. {
  8. /// <summary>
  9. /// 数据范围验证
  10. /// </summary>
  11. public class DataRangeValidationRules:ValidationRule
  12. {
  13. private decimal _minValue =0;
  14. public decimal MinValue
  15. {
  16. get { return _minValue; }
  17. set { _minValue = value; }
  18. }
  19. private decimal _maxValue=decimal.MaxValue;
  20. public decimal MaxValue
  21. {
  22. get { return _maxValue; }
  23. set { _maxValue = value; }
  24. }
  25. public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
  26. {
  27. if (value == null)
  28. {
  29. return new ValidationResult(false,null);
  30. }
  31. if (string.IsNullOrEmpty(value.ToString()))
  32. {
  33. return new ValidationResult(false, null);
  34. }
  35. if (decimal.Parse(value.ToString()) < MinValue || decimal.Parse(value.ToString()) > MaxValue)
  36. {
  37. return new ValidationResult(false, "数字范围错误!");
  38. }
  39. return new ValidationResult(true, null);
  40. }
  41. }
  42. }