|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 6 | +using CommunityToolkit.Mvvm.Input; |
| 7 | +using CommunityToolkit.WinUI; |
| 8 | + |
| 9 | +using System.ComponentModel.DataAnnotations; |
| 10 | + |
| 11 | +using INotifyDataErrorInfo = System.ComponentModel.INotifyDataErrorInfo; |
| 12 | +using DataErrorsChangedEventArgs = System.ComponentModel.DataErrorsChangedEventArgs; |
| 13 | + |
| 14 | +namespace AdornersExperiment.Samples.InputValidation; |
| 15 | + |
| 16 | +[ToolkitSample(id: nameof(InputValidationAdornerSample), "Input Validation Adorner", description: "A sample for showing how to use an Adorner for any Input Validation with INotifyDataErrorInfo.")] |
| 17 | +public sealed partial class InputValidationAdornerSample : Page |
| 18 | +{ |
| 19 | + public ValidationFormWidgetViewModel ViewModel { get; } = new(); |
| 20 | + |
| 21 | + public InputValidationAdornerSample() |
| 22 | + { |
| 23 | + this.InitializeComponent(); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/// <summary> |
| 28 | +/// ViewModel that shows using <see cref="INotifyDataErrorInfo"/> in conjunction with an Adorner. |
| 29 | +/// Via the <see cref="ObservableValidator"/> base class from the MVVM Toolkit. |
| 30 | +/// Example modified from the MVVM Toolkit Sample App. |
| 31 | +/// </summary> |
| 32 | +public partial class ValidationFormWidgetViewModel : ObservableValidator |
| 33 | +{ |
| 34 | + public event EventHandler? FormSubmissionCompleted; |
| 35 | + public event EventHandler? FormSubmissionFailed; |
| 36 | + |
| 37 | + [ObservableProperty] |
| 38 | + [Required] |
| 39 | + [MinLength(2)] |
| 40 | + [MaxLength(100)] |
| 41 | + public partial string? FirstName { get; set; } |
| 42 | + |
| 43 | + [ObservableProperty] |
| 44 | + [Required] |
| 45 | + [MinLength(2)] |
| 46 | + [MaxLength(100)] |
| 47 | + public partial string? LastName { get; set; } |
| 48 | + |
| 49 | + [ObservableProperty] |
| 50 | + [Required] |
| 51 | + [EmailAddress] |
| 52 | + public partial string? Email { get; set; } |
| 53 | + |
| 54 | + [ObservableProperty] |
| 55 | + [Required] |
| 56 | + [Phone] |
| 57 | + public partial string? PhoneNumber { get; set; } |
| 58 | + |
| 59 | + [ObservableProperty] |
| 60 | + [Required] |
| 61 | + [Range(13, 120)] |
| 62 | + public partial int Age { get; set; } |
| 63 | + |
| 64 | + [RelayCommand] |
| 65 | + private void Submit() |
| 66 | + { |
| 67 | + ValidateAllProperties(); |
| 68 | + |
| 69 | + if (HasErrors) |
| 70 | + { |
| 71 | + FormSubmissionFailed?.Invoke(this, EventArgs.Empty); |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + FormSubmissionCompleted?.Invoke(this, EventArgs.Empty); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// <summary> |
| 81 | +/// An Adorner that shows an error message if Data Validation fails. |
| 82 | +/// The adorned control's <see cref="FrameworkElement.DataContext"/> must implement <see cref="INotifyDataErrorInfo"/>. It assumes that the return of <see cref="INotifyDataErrorInfo.GetErrors(string?)"/> is a <see cref="ValidationResult"/> collection. |
| 83 | +/// </summary> |
| 84 | +public sealed partial class InputValidationAdorner : Adorner<FrameworkElement> |
| 85 | +{ |
| 86 | + /// <summary> |
| 87 | + /// Gets or sets the name of the property this adorner should look for errors on. |
| 88 | + /// </summary> |
| 89 | + public string PropertyName |
| 90 | + { |
| 91 | + get { return (string)GetValue(PropertyNameProperty); } |
| 92 | + set { SetValue(PropertyNameProperty, value); } |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Identifies the <see cref="PropertyName"/> dependency property. |
| 97 | + /// </summary> |
| 98 | + public static readonly DependencyProperty PropertyNameProperty = |
| 99 | + DependencyProperty.Register(nameof(PropertyName), typeof(string), typeof(InputValidationAdorner), new PropertyMetadata(null)); |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Gets or sets whether the popup is open. |
| 103 | + /// </summary> |
| 104 | + public bool HasValidationFailed |
| 105 | + { |
| 106 | + get { return (bool)GetValue(HasValidationFailedProperty); } |
| 107 | + set { SetValue(HasValidationFailedProperty, value); } |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Identifies the <see cref="HasValidationFailed"/> dependency property. |
| 112 | + /// </summary> |
| 113 | + public static readonly DependencyProperty HasValidationFailedProperty = |
| 114 | + DependencyProperty.Register(nameof(HasValidationFailed), typeof(bool), typeof(InputValidationAdorner), new PropertyMetadata(false)); |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Gets or sets the validation message for this failed property. |
| 118 | + /// </summary> |
| 119 | + public string ValidationMessage |
| 120 | + { |
| 121 | + get { return (string)GetValue(ValidationMessageProperty); } |
| 122 | + set { SetValue(ValidationMessageProperty, value); } |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Identifies the <see cref="ValidationMessage"/> dependency property. |
| 127 | + /// </summary> |
| 128 | + public static readonly DependencyProperty ValidationMessageProperty = |
| 129 | + DependencyProperty.Register(nameof(ValidationMessage), typeof(string), typeof(InputValidationAdorner), new PropertyMetadata(null)); |
| 130 | + |
| 131 | + private INotifyDataErrorInfo? _dataErrorInfo; |
| 132 | + |
| 133 | + public InputValidationAdorner() |
| 134 | + { |
| 135 | + this.DefaultStyleKey = typeof(InputValidationAdorner); |
| 136 | + |
| 137 | + // Uno workaround |
| 138 | + DataContext = this; |
| 139 | + } |
| 140 | + |
| 141 | + protected override void OnApplyTemplate() |
| 142 | + { |
| 143 | + base.OnApplyTemplate(); |
| 144 | + } |
| 145 | + |
| 146 | + protected override void OnAttached() |
| 147 | + { |
| 148 | + base.OnAttached(); |
| 149 | + |
| 150 | + if (AdornedElement?.DataContext is INotifyDataErrorInfo iError) |
| 151 | + { |
| 152 | + _dataErrorInfo = iError; |
| 153 | + _dataErrorInfo.ErrorsChanged += this.INotifyDataErrorInfo_ErrorsChanged; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private void INotifyDataErrorInfo_ErrorsChanged(object? sender, DataErrorsChangedEventArgs e) |
| 158 | + { |
| 159 | + if (_dataErrorInfo is not null) |
| 160 | + { |
| 161 | + // Reset state |
| 162 | + if (!_dataErrorInfo.HasErrors) |
| 163 | + { |
| 164 | + HasValidationFailed = false; |
| 165 | + ValidationMessage = string.Empty; |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + if (e.PropertyName == PropertyName) |
| 170 | + { |
| 171 | + HasValidationFailed = true; |
| 172 | + |
| 173 | + StringBuilder message = new(); |
| 174 | + foreach (ValidationResult result in _dataErrorInfo.GetErrors(e.PropertyName)) |
| 175 | + { |
| 176 | + message.AppendLine(result.ErrorMessage); |
| 177 | + } |
| 178 | + |
| 179 | + ValidationMessage = message.ToString().Trim(); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + protected override void OnDetaching() |
| 185 | + { |
| 186 | + if (_dataErrorInfo is not null) |
| 187 | + { |
| 188 | + _dataErrorInfo.ErrorsChanged -= this.INotifyDataErrorInfo_ErrorsChanged; |
| 189 | + _dataErrorInfo = null; |
| 190 | + } |
| 191 | + |
| 192 | + base.OnDetaching(); |
| 193 | + } |
| 194 | +} |
0 commit comments