-
Notifications
You must be signed in to change notification settings - Fork 6
/
AsyncValidationFaultControl.cs
89 lines (78 loc) · 3.29 KB
/
AsyncValidationFaultControl.cs
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
using DevZest.Data.Presenters;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace DevZest.Data.Views.Primitives
{
/// <summary>
/// Represents the control that displays and retries the async validation fault.
/// </summary>
/// <remarks></remarks>
public class AsyncValidationFaultControl : Control
{
/// <summary>
/// Contains commands supported by <see cref="AsyncValidationFaultControl"/>.
/// </summary>
public static class Commands
{
/// <summary>
/// Command to retry async validation.
/// </summary>
public static readonly RoutedUICommand Retry = new RoutedUICommand(UserMessages.AsyncValidationFaultControlCommands_RetryCommandText, nameof(Retry), typeof(Commands));
}
/// <summary>
/// Identifies the <see cref="Fault"/> dependency property.
/// </summary>
public static readonly DependencyProperty FaultProperty = DependencyProperty.Register(nameof(Fault), typeof(AsyncValidationFault), typeof(AsyncValidationFaultControl),
new FrameworkPropertyMetadata(null, OnFaultChanged));
private static readonly DependencyPropertyKey MessagePropertyKey = DependencyProperty.RegisterReadOnly(nameof(Message), typeof(string), typeof(AsyncValidationFaultControl),
new FrameworkPropertyMetadata(null));
/// <summary>
/// Identifies the <see cref="Message"/> dependency property.
/// </summary>
public static readonly DependencyProperty MessageProperty = MessagePropertyKey.DependencyProperty;
static AsyncValidationFaultControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(AsyncValidationFaultControl), new FrameworkPropertyMetadata(typeof(AsyncValidationFaultControl)));
}
private static void OnFaultChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((AsyncValidationFaultControl)d).Message = ((AsyncValidationFault)e.NewValue)?.Message;
}
/// <summary>
/// Initializes a new instance of <see cref="AsyncValidationFaultControl"/>.
/// </summary>
public AsyncValidationFaultControl()
{
CommandBindings.Add(new CommandBinding(Commands.Retry, ExecRetry, CanRetry));
}
/// <summary>
/// Gets or sets the async validation fault. This is a dependency property.
/// </summary>
public AsyncValidationFault Fault
{
get { return (AsyncValidationFault)GetValue(FaultProperty); }
set { SetValue(FaultProperty, value); }
}
/// <summary>
/// Gets the error message. This is a depdency property.
/// </summary>
public string Message
{
get { return (string)GetValue(MessageProperty); }
private set { SetValue(MessagePropertyKey, value); }
}
private AsyncValidator Validator
{
get { return Fault?.Source; }
}
private void ExecRetry(object sender, ExecutedRoutedEventArgs e)
{
Validator.Run();
}
private void CanRetry(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = Validator != null;
}
}
}