|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 |
|
| 5 | +using System.ComponentModel; |
| 6 | +using System.ComponentModel.DataAnnotations; |
| 7 | +using System.Linq; |
5 | 8 | using Windows.UI.Xaml;
|
6 | 9 | using Windows.UI.Xaml.Controls;
|
7 |
| -using Windows.UI.Xaml.Input; |
8 | 10 |
|
9 | 11 | namespace MvvmSampleUwp.Controls;
|
10 | 12 |
|
11 | 13 | /// <summary>
|
12 | 14 | /// A simple control that acts as a container for a documentation block.
|
13 | 15 | /// </summary>
|
| 16 | +[TemplatePart(Name = "PART_TextBox", Type = typeof(TextBox))] |
| 17 | +[TemplatePart(Name = "PART_WarningIcon", Type = typeof(FontIcon))] |
14 | 18 | public sealed class ValidationTextBox : ContentControl
|
15 | 19 | {
|
| 20 | + /// <summary> |
| 21 | + /// The <see cref="TextBox"/> instance in use. |
| 22 | + /// </summary> |
| 23 | + private TextBox textBox; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// The <see cref="MarkdownTextBlock"/> instance in use. |
| 27 | + /// </summary> |
| 28 | + private FontIcon warningIcon; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// The previous data context in use. |
| 32 | + /// </summary> |
| 33 | + private INotifyDataErrorInfo oldDataContext; |
| 34 | + |
| 35 | + public ValidationTextBox() |
| 36 | + { |
| 37 | + DataContextChanged += ValidationTextBox_DataContextChanged; |
| 38 | + } |
| 39 | + |
| 40 | + /// <inheritdoc/> |
| 41 | + protected override void OnApplyTemplate() |
| 42 | + { |
| 43 | + base.OnApplyTemplate(); |
| 44 | + |
| 45 | + textBox = (TextBox)GetTemplateChild("PART_TextBox"); |
| 46 | + warningIcon = (FontIcon)GetTemplateChild("PART_WarningIcon"); |
| 47 | + |
| 48 | + textBox.TextChanged += TextBox_TextChanged; |
| 49 | + } |
| 50 | + |
16 | 51 | /// <summary>
|
17 | 52 | /// Gets or sets the <see cref="string"/> representing the text to display.
|
18 | 53 | /// </summary>
|
@@ -68,20 +103,91 @@ public string PlaceholderText
|
68 | 103 | new PropertyMetadata(default(string)));
|
69 | 104 |
|
70 | 105 | /// <summary>
|
71 |
| - /// Gets or sets the <see cref="string"/> representing the input scope to use. |
| 106 | + /// Gets or sets the <see cref="string"/> representing the text to display. |
72 | 107 | /// </summary>
|
73 |
| - public InputScope InputScope |
| 108 | + public string PropertyName |
74 | 109 | {
|
75 |
| - get => (InputScope)GetValue(InputScopeProperty); |
76 |
| - set => SetValue(InputScopeProperty, value); |
| 110 | + get => (string)GetValue(PropertyNameProperty); |
| 111 | + set => SetValue(PropertyNameProperty, value); |
77 | 112 | }
|
78 | 113 |
|
79 | 114 | /// <summary>
|
80 |
| - /// The <see cref="DependencyProperty"/> backing <see cref="InputScope"/>. |
| 115 | + /// The <see cref="DependencyProperty"/> backing <see cref="PropertyName"/>. |
81 | 116 | /// </summary>
|
82 |
| - public static readonly DependencyProperty InputScopeProperty = DependencyProperty.Register( |
83 |
| - nameof(InputScope), |
84 |
| - typeof(InputScope), |
| 117 | + public static readonly DependencyProperty PropertyNameProperty = DependencyProperty.Register( |
| 118 | + nameof(PropertyName), |
| 119 | + typeof(string), |
85 | 120 | typeof(ValidationTextBox),
|
86 |
| - new PropertyMetadata(default(InputScope))); |
| 121 | + new PropertyMetadata(PropertyNameProperty, OnPropertyNamePropertyChanged)); |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Invokes <see cref="RefreshErrors"/> whenever <see cref="PropertyName"/> changes. |
| 125 | + /// </summary> |
| 126 | + private static void OnPropertyNamePropertyChanged(object sender, DependencyPropertyChangedEventArgs args) |
| 127 | + { |
| 128 | + if (args.NewValue is not string { Length: > 0 } propertyName) |
| 129 | + { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + ((ValidationTextBox)sender).RefreshErrors(); |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Updates the bindings whenever the data context changes. |
| 138 | + /// </summary> |
| 139 | + private void ValidationTextBox_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args) |
| 140 | + { |
| 141 | + if (oldDataContext is not null) |
| 142 | + { |
| 143 | + oldDataContext.ErrorsChanged -= DataContext_ErrorsChanged; |
| 144 | + } |
| 145 | + |
| 146 | + if (args.NewValue is INotifyDataErrorInfo dataContext) |
| 147 | + { |
| 148 | + oldDataContext = dataContext; |
| 149 | + |
| 150 | + oldDataContext.ErrorsChanged += DataContext_ErrorsChanged; |
| 151 | + } |
| 152 | + |
| 153 | + RefreshErrors(); |
| 154 | + } |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// Invokes <see cref="RefreshErrors"/> whenever the data context requires it. |
| 158 | + /// </summary> |
| 159 | + private void DataContext_ErrorsChanged(object sender, DataErrorsChangedEventArgs e) |
| 160 | + { |
| 161 | + RefreshErrors(); |
| 162 | + } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Updates <see cref="Text"/> when needed. |
| 166 | + /// </summary> |
| 167 | + private void TextBox_TextChanged(object sender, TextChangedEventArgs e) |
| 168 | + { |
| 169 | + Text = ((TextBox)sender).Text; |
| 170 | + } |
| 171 | + |
| 172 | + /// <summary> |
| 173 | + /// Refreshes the errors currently displayed. |
| 174 | + /// </summary> |
| 175 | + private void RefreshErrors() |
| 176 | + { |
| 177 | + if (this.warningIcon is not FontIcon warningIcon || |
| 178 | + PropertyName is not string propertyName || |
| 179 | + DataContext is not INotifyDataErrorInfo dataContext) |
| 180 | + { |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + ValidationResult result = dataContext.GetErrors(propertyName).OfType<ValidationResult>().FirstOrDefault(); |
| 185 | + |
| 186 | + warningIcon.Visibility = result is not null ? Visibility.Visible : Visibility.Collapsed; |
| 187 | + |
| 188 | + if (result is not null) |
| 189 | + { |
| 190 | + ToolTipService.SetToolTip(warningIcon, result.ErrorMessage); |
| 191 | + } |
| 192 | + } |
87 | 193 | }
|
0 commit comments