|
1 | 1 | using System;
|
2 |
| -using System.Collections.Generic; |
3 |
| -using System.Linq; |
4 |
| -using System.Text; |
5 |
| -using System.Threading.Tasks; |
6 | 2 | using System.Windows;
|
7 | 3 | using System.Windows.Controls;
|
8 | 4 | using System.Windows.Controls.Primitives;
|
|
11 | 7 | namespace MaterialDesignThemes.Wpf
|
12 | 8 | {
|
13 | 9 | /// <summary>
|
14 |
| - /// MahApp's validation popup copy |
| 10 | + /// Customised popup for validation purposes. |
15 | 11 | /// </summary>
|
16 |
| - public sealed class CustomValidationPopup : Popup |
| 12 | + /// <remarks> |
| 13 | + /// Originally taken from MahApps. |
| 14 | + /// </remarks> |
| 15 | + public class CustomValidationPopup : Popup |
17 | 16 | {
|
18 | 17 | private Window _hostWindow;
|
19 | 18 |
|
20 | 19 | public CustomValidationPopup()
|
21 | 20 | {
|
22 |
| - this.Loaded += this.CustomValidationPopup_Loaded; |
23 |
| - this.Opened += this.CustomValidationPopup_Opened; |
| 21 | + Loaded += OnLoaded; |
| 22 | + Unloaded += OnUnloaded; |
24 | 23 | }
|
25 | 24 |
|
26 | 25 | protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
|
27 | 26 | {
|
28 |
| - this.IsOpen = false; |
| 27 | + IsOpen = false; |
29 | 28 | }
|
30 | 29 |
|
31 |
| - private void CustomValidationPopup_Loaded(object sender, RoutedEventArgs e) |
| 30 | + private void OnLoaded(object sender, RoutedEventArgs e) |
32 | 31 | {
|
33 |
| - var target = this.PlacementTarget as FrameworkElement; |
34 |
| - if (target == null) |
35 |
| - { |
36 |
| - return; |
37 |
| - } |
38 |
| - |
39 |
| - this._hostWindow = Window.GetWindow(target); |
40 |
| - if (this._hostWindow == null) |
41 |
| - { |
42 |
| - return; |
43 |
| - } |
44 |
| - |
45 |
| - this._hostWindow.LocationChanged -= this.hostWindow_SizeOrLocationChanged; |
46 |
| - this._hostWindow.LocationChanged += this.hostWindow_SizeOrLocationChanged; |
47 |
| - this._hostWindow.SizeChanged -= this.hostWindow_SizeOrLocationChanged; |
48 |
| - this._hostWindow.SizeChanged += this.hostWindow_SizeOrLocationChanged; |
49 |
| - target.SizeChanged -= this.hostWindow_SizeOrLocationChanged; |
50 |
| - target.SizeChanged += this.hostWindow_SizeOrLocationChanged; |
51 |
| - this._hostWindow.StateChanged -= this.hostWindow_StateChanged; |
52 |
| - this._hostWindow.StateChanged += this.hostWindow_StateChanged; |
53 |
| - this._hostWindow.Activated -= this.hostWindow_Activated; |
54 |
| - this._hostWindow.Activated += this.hostWindow_Activated; |
55 |
| - this._hostWindow.Deactivated -= this.hostWindow_Deactivated; |
56 |
| - this._hostWindow.Deactivated += this.hostWindow_Deactivated; |
57 |
| - |
58 |
| - this.Unloaded -= this.CustomValidationPopup_Unloaded; |
59 |
| - this.Unloaded += this.CustomValidationPopup_Unloaded; |
60 |
| - } |
| 32 | + var target = PlacementTarget as FrameworkElement; |
| 33 | + if (target == null) return; |
61 | 34 |
|
62 |
| - private void CustomValidationPopup_Opened(object sender, EventArgs e) |
63 |
| - { |
64 |
| - //this.SetTopmostState(true); |
65 |
| - } |
| 35 | + target.SizeChanged += HostWindowSizeOrLocationChanged; |
66 | 36 |
|
67 |
| - private void hostWindow_Activated(object sender, EventArgs e) |
68 |
| - { |
69 |
| - //this.SetTopmostState(true); |
70 |
| - } |
| 37 | + _hostWindow = Window.GetWindow(target); |
| 38 | + if (_hostWindow == null) return; |
71 | 39 |
|
72 |
| - private void hostWindow_Deactivated(object sender, EventArgs e) |
73 |
| - { |
74 |
| - //this.SetTopmostState(false); |
| 40 | + _hostWindow.LocationChanged += HostWindowSizeOrLocationChanged; |
| 41 | + _hostWindow.SizeChanged += HostWindowSizeOrLocationChanged; |
| 42 | + |
| 43 | + _hostWindow.StateChanged += HostWindowOnStateChanged; |
75 | 44 | }
|
76 | 45 |
|
77 |
| - private void CustomValidationPopup_Unloaded(object sender, RoutedEventArgs e) |
| 46 | + private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs) |
78 | 47 | {
|
79 |
| - var target = this.PlacementTarget as FrameworkElement; |
| 48 | + var target = PlacementTarget as FrameworkElement; |
80 | 49 | if (target != null)
|
81 |
| - { |
82 |
| - target.SizeChanged -= this.hostWindow_SizeOrLocationChanged; |
83 |
| - } |
84 |
| - if (this._hostWindow != null) |
85 |
| - { |
86 |
| - this._hostWindow.LocationChanged -= this.hostWindow_SizeOrLocationChanged; |
87 |
| - this._hostWindow.SizeChanged -= this.hostWindow_SizeOrLocationChanged; |
88 |
| - this._hostWindow.StateChanged -= this.hostWindow_StateChanged; |
89 |
| - this._hostWindow.Activated -= this.hostWindow_Activated; |
90 |
| - this._hostWindow.Deactivated -= this.hostWindow_Deactivated; |
91 |
| - } |
92 |
| - this.Unloaded -= this.CustomValidationPopup_Unloaded; |
93 |
| - this.Opened -= this.CustomValidationPopup_Opened; |
94 |
| - this._hostWindow = null; |
| 50 | + target.SizeChanged -= HostWindowSizeOrLocationChanged; |
| 51 | + |
| 52 | + if (_hostWindow == null) return; |
| 53 | + |
| 54 | + _hostWindow.LocationChanged -= HostWindowSizeOrLocationChanged; |
| 55 | + _hostWindow.SizeChanged -= HostWindowSizeOrLocationChanged; |
| 56 | + _hostWindow.StateChanged -= HostWindowOnStateChanged; |
95 | 57 | }
|
96 | 58 |
|
97 |
| - private void hostWindow_StateChanged(object sender, EventArgs e) |
| 59 | + private void HostWindowOnStateChanged(object sender, EventArgs e) |
98 | 60 | {
|
99 |
| - if (this._hostWindow != null && this._hostWindow.WindowState != WindowState.Minimized) |
100 |
| - { |
101 |
| - var target = this.PlacementTarget as FrameworkElement; |
102 |
| - var holder = target != null ? target.DataContext as AdornedElementPlaceholder : null; |
103 |
| - if (holder != null && holder.AdornedElement != null) |
104 |
| - { |
105 |
| - var errorTemplate = holder.AdornedElement.GetValue(Validation.ErrorTemplateProperty); |
106 |
| - holder.AdornedElement.SetValue(Validation.ErrorTemplateProperty, null); |
107 |
| - holder.AdornedElement.SetValue(Validation.ErrorTemplateProperty, errorTemplate); |
108 |
| - } |
109 |
| - } |
| 61 | + if (_hostWindow == null || _hostWindow.WindowState == WindowState.Minimized) return; |
| 62 | + |
| 63 | + var target = PlacementTarget as FrameworkElement; |
| 64 | + var holder = target?.DataContext as AdornedElementPlaceholder; |
| 65 | + |
| 66 | + if (holder?.AdornedElement == null) return; |
| 67 | + |
| 68 | + var errorTemplate = holder.AdornedElement.GetValue(Validation.ErrorTemplateProperty); |
| 69 | + holder.AdornedElement.SetValue(Validation.ErrorTemplateProperty, null); |
| 70 | + holder.AdornedElement.SetValue(Validation.ErrorTemplateProperty, errorTemplate); |
110 | 71 | }
|
111 | 72 |
|
112 |
| - private void hostWindow_SizeOrLocationChanged(object sender, EventArgs e) |
| 73 | + private void HostWindowSizeOrLocationChanged(object sender, EventArgs e) |
113 | 74 | {
|
114 |
| - var offset = this.HorizontalOffset; |
| 75 | + var offset = HorizontalOffset; |
115 | 76 | // "bump" the offset to cause the popup to reposition itself on its own
|
116 |
| - this.HorizontalOffset = offset + 1; |
117 |
| - this.HorizontalOffset = offset; |
| 77 | + HorizontalOffset = offset + 1; |
| 78 | + HorizontalOffset = offset; |
118 | 79 | }
|
119 | 80 | }
|
120 | 81 | }
|
0 commit comments