-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationContainer.cs
More file actions
235 lines (186 loc) · 9.53 KB
/
Copy pathValidationContainer.cs
File metadata and controls
235 lines (186 loc) · 9.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace VisualValidation
{
/// <summary>
/// ValidationContainer is a light weight validation framework which only takes care displaying visaul effect of validation results for each validation field.
/// Don't use it to control Navigation. View Model should control it.
/// </summary>
public class ValidationContainer : ContentControl, IValidationContainer
{
/// <summary>
/// used internally by valiation framework.
/// </summary>
public static readonly DependencyProperty ValidationFieldHandlerProperty =
DependencyProperty.RegisterAttached("ValidationFieldHandler",
typeof(ValidationFieldHandler),
typeof(ValidationContainer),
new FrameworkPropertyMetadata(null));
/// <summary>
/// The name of a property of the IValidationSource. When this IValidationSource notifies this property changed,
/// ValidationContainer will validate this field with IValiationSource and set ValidationResult to the binded UI control.
/// </summary>
public static readonly DependencyProperty ValidationFieldProperty =
DependencyProperty.RegisterAttached("ValidationField", typeof(string), typeof(ValidationContainer),
new PropertyMetadata(string.Empty, OnValidateFieldSet));
/// <summary>
/// Can be used to display validation result for the binded UI control.
/// </summary>
public static readonly DependencyProperty ValidationResultProperty =
DependencyProperty.RegisterAttached("ValidationResult",
typeof(ValidationResult),
typeof(ValidationContainer),
new PropertyMetadata(null, OnValidationResultSet));
/// <summary>
/// An object of IValidationSource which provide vilidation and normally is View Model.
/// </summary>
public static readonly DependencyProperty ValidationSourceProperty =
DependencyProperty.Register("ValidationSource", typeof(IValidationSource),
typeof(ValidationContainer),
new FrameworkPropertyMetadata(null, OnValidationSourcePropertySet));
public static readonly DependencyProperty ValidationEnabledProperty =
DependencyProperty.Register("ValidationEnabled", typeof(bool),
typeof(ValidationContainer),
new PropertyMetadata(false, OnValidationEnabledSet));
public static readonly DependencyProperty ValidationTemplateProperty = DependencyProperty.RegisterAttached("ValidationTemplate",
typeof(ControlTemplate), typeof(ValidationContainer), new PropertyMetadata(null));
public static ControlTemplate GetValidationTemplate(DependencyObject obj)
{
return (ControlTemplate)obj.GetValue(ValidationTemplateProperty);
}
public static void SetValidationTemplate(DependencyObject obj, ControlTemplate value)
{
obj.SetValue(ValidationTemplateProperty, value);
}
internal static readonly DependencyProperty ValidationErrorAdornerProperty = DependencyProperty.RegisterAttached("ValidationErrorAdorner",
typeof(Adorner), typeof(ValidationContainer), new UIPropertyMetadata(null));
#region IValidationContainer Members
//not strong type it as IValidationSource, in order to avoid exception when binding is not ready.
public IValidationSource ValidationSource
{
get { return GetValue(ValidationSourceProperty) as IValidationSource; }
set
{
SetValue(ValidationSourceProperty, value);
ValidationSourceChanged.Invoke(this, null);
}
}
public event EventHandler ValidationSourceChanged = delegate { };
public event EventHandler ValidationEnabledChanged = delegate { };
public bool ValidationEnabled
{
get { return (bool)GetValue(ValidationEnabledProperty); }
set
{
SetValue(ValidationEnabledProperty, value);
ValidationEnabledChanged.Invoke(this, null);
}
}
#endregion
public static string GetValidationField(DependencyObject d)
{
return (string)d.GetValue(ValidationFieldProperty);
}
public static void SetValidationField(DependencyObject d, string value)
{
d.SetValue(ValidationFieldProperty, value);
}
public static ValidationResult GetValidationResult(DependencyObject d)
{
return (ValidationResult)d.GetValue(ValidationResultProperty);
}
public static void SetValidationResult(DependencyObject d, ValidationResult value)
{
d.SetValue(ValidationResultProperty, value);
}
private static void OnValidationSourcePropertySet(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var container = d as ValidationContainer;
if (container != null)
{
container.NotifyValidationSourceChanged();
Debug.WriteLine(string.Format("Validation source {0} set on {1}", e.NewValue, container));
}
}
private static void OnValidateFieldSet(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var uiElement = d as FrameworkElement;
if (uiElement == null) throw new ApplicationException("ValidationField property should be set on a FrameworkElement.");
if (uiElement.IsLoaded) throw new ApplicationException("ValidationField property should not be set after it is loaded.");
var filed = e.NewValue as string;
var oldValidationFieldHandler = uiElement.GetValue(ValidationFieldHandlerProperty) as ValidationFieldHandler;
if (oldValidationFieldHandler != null)
{
oldValidationFieldHandler.UnRegisterEventFromValidationSource();
uiElement.SetValue(ValidationFieldHandlerProperty, null);
}
uiElement.Loaded += (s, arg) => SetValidationHandler(uiElement, filed);
}
private static void OnValidationEnabledSet(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var container = d as ValidationContainer;
if (container != null)
{
container.NotifyValidationEnabledChanged();
Debug.WriteLine(string.Format("ValidationEnabled {0} set on {1}", e.NewValue, container));
}
}
private static void SetValidationHandler(FrameworkElement uiElement, string filed)
{
var oldValidationFieldHandler = uiElement.GetValue(ValidationFieldHandlerProperty) as ValidationFieldHandler;
if (oldValidationFieldHandler == null) //only need set once
{
IValidationContainer validationContainer = new UiTreeHelper().FindValidationContainer(uiElement);
var validationFieldHandler = new ValidationFieldHandler(new UIElementWrapper(uiElement), filed, validationContainer);
uiElement.SetValue(ValidationFieldHandlerProperty, validationFieldHandler);
}
}
private static void OnValidationResultSet(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var uiElement = d as FrameworkElement;
if (uiElement == null) throw new ApplicationException("ValidationResult property should be set on a FrameworkElement.");
ReoveAdorner(uiElement);
var validationResult = e.NewValue as ValidationResult;
if (validationResult != null)
{
if (!validationResult.IsValid)
AddAdorner(uiElement, e.NewValue);
}
}
private static void AddAdorner(FrameworkElement element, object dataContext)
{
ControlTemplate template = GetValidationTemplate(element);
if (template == null) return;
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(element);
if (adornerLayer != null)
{
var adorner = new TemplatedAdorner(element, dataContext, template);
adornerLayer.Add(adorner);
element.SetValue(ValidationErrorAdornerProperty, adorner);
}
}
private static void ReoveAdorner(FrameworkElement element)
{
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(element);
if (adornerLayer != null)
{
var oldAdorner = element.GetValue(ValidationErrorAdornerProperty) as Adorner;
if (oldAdorner != null)
{
adornerLayer.Remove(oldAdorner);
}
}
}
private void NotifyValidationEnabledChanged()
{
ValidationEnabledChanged.Invoke(this, null);
}
private void NotifyValidationSourceChanged()
{
ValidationSourceChanged.Invoke(this, null);
}
}
}