25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

117 satır
4.1 KiB

  1. using BPA.UIControl.Commons.KnownBoxes;
  2. using System;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. namespace BPA.UIControl
  8. {
  9. /// <summary>
  10. /// 验证表单,只用于判断子控件是否有验证错误信息
  11. /// </summary>
  12. public class ValidationForm : Border
  13. {
  14. private readonly ObservableCollection<ValidationError> errors = new ObservableCollection<ValidationError>();
  15. static ValidationForm()
  16. {
  17. DefaultStyleKeyProperty.OverrideMetadata(typeof(ValidationForm), new FrameworkPropertyMetadata(typeof(ValidationForm)));
  18. }
  19. internal static readonly DependencyPropertyKey IsValidPropertyKey = DependencyProperty.RegisterReadOnly(
  20. "IsValid", typeof(bool), typeof(ValidationForm), new FrameworkPropertyMetadata(BooleanBoxes.TrueBox));
  21. /// <summary>
  22. /// 是否有效
  23. /// </summary>
  24. public static readonly DependencyProperty IsValidProperty = IsValidPropertyKey.DependencyProperty;
  25. /// <summary>
  26. /// 是否有效
  27. /// </summary>
  28. public bool IsValid
  29. {
  30. get { return (bool)GetValue(IsValidProperty); }
  31. private set { SetValue(IsValidPropertyKey, BooleanBoxes.Box(value)); }
  32. }
  33. internal static readonly DependencyPropertyKey ErrorsPropertyKey = DependencyProperty.RegisterReadOnly(
  34. "Errors", typeof(ReadOnlyObservableCollection<ValidationError>), typeof(ValidationForm), new FrameworkPropertyMetadata(new ReadOnlyObservableCollection<ValidationError>(new ObservableCollection<ValidationError>()), FrameworkPropertyMetadataOptions.NotDataBindable));
  35. /// <summary>
  36. /// 所有错误
  37. /// </summary>
  38. public static readonly DependencyProperty ErrorsProperty = ErrorsPropertyKey.DependencyProperty;
  39. /// <summary>
  40. /// 所有错误
  41. /// </summary>
  42. public ReadOnlyObservableCollection<ValidationError> Errors
  43. {
  44. get { return (ReadOnlyObservableCollection<ValidationError>)GetValue(ErrorsProperty); }
  45. private set { SetValue(ErrorsPropertyKey, value); }
  46. }
  47. internal static readonly DependencyPropertyKey FirstErrorPropertyKey = DependencyProperty.RegisterReadOnly(
  48. "FirstError", typeof(ValidationError), typeof(ValidationForm), new FrameworkPropertyMetadata(default(ValidationError)));
  49. /// <summary>
  50. /// 所有错误
  51. /// </summary>
  52. public static readonly DependencyProperty FirstErrorProperty = FirstErrorPropertyKey.DependencyProperty;
  53. /// <summary>
  54. /// 所有错误
  55. /// </summary>
  56. public ValidationError FirstError
  57. {
  58. get { return (ValidationError)GetValue(FirstErrorProperty); }
  59. private set { SetValue(FirstErrorPropertyKey, value); }
  60. }
  61. /// <inheritdoc/>
  62. protected override void OnInitialized(EventArgs e)
  63. {
  64. base.OnInitialized(e);
  65. AddValidationDependencyObject(this);
  66. }
  67. private void AddValidationDependencyObject(DependencyObject element)
  68. {
  69. if (element == null)
  70. {
  71. return;
  72. }
  73. if (element is TextBox || element is PasswordBox)
  74. {
  75. Validation.AddErrorHandler(element, OnValidationError);
  76. }
  77. foreach (var child in LogicalTreeHelper.GetChildren(element))
  78. {
  79. AddValidationDependencyObject(child as DependencyObject);
  80. }
  81. }
  82. private void OnValidationError(object sender, ValidationErrorEventArgs e)
  83. {
  84. switch (e.Action)
  85. {
  86. case ValidationErrorEventAction.Added:
  87. errors.Add(e.Error);
  88. break;
  89. case ValidationErrorEventAction.Removed:
  90. errors.Remove(e.Error);
  91. break;
  92. }
  93. Errors = new ReadOnlyObservableCollection<ValidationError>(errors);
  94. FirstError = Errors.FirstOrDefault();
  95. IsValid = Errors.Count == 0;
  96. }
  97. }
  98. }