|
| 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 System.ComponentModel; |
| 6 | +using System.ComponentModel.DataAnnotations; |
| 7 | +using System.Linq; |
| 8 | +using Windows.UI.Xaml; |
| 9 | +using Windows.UI.Xaml.Controls; |
| 10 | + |
| 11 | +namespace MvvmSampleUwp.Controls; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// A simple control that acts as a container for a documentation block. |
| 15 | +/// </summary> |
| 16 | +[TemplatePart(Name = "PART_TextBox", Type = typeof(TextBox))] |
| 17 | +[TemplatePart(Name = "PART_WarningIcon", Type = typeof(FontIcon))] |
| 18 | +public sealed class ValidationTextBox : ContentControl |
| 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 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets or sets the <see cref="string"/> representing the text to display. |
| 53 | + /// </summary> |
| 54 | + public string Text |
| 55 | + { |
| 56 | + get => (string)GetValue(TextProperty); |
| 57 | + set => SetValue(TextProperty, value); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// The <see cref="DependencyProperty"/> backing <see cref="Text"/>. |
| 62 | + /// </summary> |
| 63 | + public static readonly DependencyProperty TextProperty = DependencyProperty.Register( |
| 64 | + nameof(Text), |
| 65 | + typeof(string), |
| 66 | + typeof(ValidationTextBox), |
| 67 | + new PropertyMetadata(default(string))); |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Gets or sets the <see cref="string"/> representing the header text to display. |
| 71 | + /// </summary> |
| 72 | + public string HeaderText |
| 73 | + { |
| 74 | + get => (string)GetValue(HeaderTextProperty); |
| 75 | + set => SetValue(HeaderTextProperty, value); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// The <see cref="DependencyProperty"/> backing <see cref="HeaderText"/>. |
| 80 | + /// </summary> |
| 81 | + public static readonly DependencyProperty HeaderTextProperty = DependencyProperty.Register( |
| 82 | + nameof(HeaderText), |
| 83 | + typeof(string), |
| 84 | + typeof(ValidationTextBox), |
| 85 | + new PropertyMetadata(default(string))); |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Gets or sets the <see cref="string"/> representing the placeholder text to display. |
| 89 | + /// </summary> |
| 90 | + public string PlaceholderText |
| 91 | + { |
| 92 | + get => (string)GetValue(PlaceholderTextProperty); |
| 93 | + set => SetValue(PlaceholderTextProperty, value); |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// The <see cref="DependencyProperty"/> backing <see cref="PlaceholderText"/>. |
| 98 | + /// </summary> |
| 99 | + public static readonly DependencyProperty PlaceholderTextProperty = DependencyProperty.Register( |
| 100 | + nameof(PlaceholderText), |
| 101 | + typeof(string), |
| 102 | + typeof(ValidationTextBox), |
| 103 | + new PropertyMetadata(default(string))); |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Gets or sets the <see cref="string"/> representing the text to display. |
| 107 | + /// </summary> |
| 108 | + public string PropertyName |
| 109 | + { |
| 110 | + get => (string)GetValue(PropertyNameProperty); |
| 111 | + set => SetValue(PropertyNameProperty, value); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// The <see cref="DependencyProperty"/> backing <see cref="PropertyName"/>. |
| 116 | + /// </summary> |
| 117 | + public static readonly DependencyProperty PropertyNameProperty = DependencyProperty.Register( |
| 118 | + nameof(PropertyName), |
| 119 | + typeof(string), |
| 120 | + typeof(ValidationTextBox), |
| 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 | + } |
| 193 | +} |
0 commit comments