Skip to content

Commit e2e33d9

Browse files
committed
standards tidy up, delete dead wood
1 parent e0ef525 commit e2e33d9

File tree

4 files changed

+49
-88
lines changed

4 files changed

+49
-88
lines changed
Lines changed: 42 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62
using System.Windows;
73
using System.Windows.Controls;
84
using System.Windows.Controls.Primitives;
@@ -11,110 +7,75 @@
117
namespace MaterialDesignThemes.Wpf
128
{
139
/// <summary>
14-
/// MahApp's validation popup copy
10+
/// Customised popup for validation purposes.
1511
/// </summary>
16-
public sealed class CustomValidationPopup : Popup
12+
/// <remarks>
13+
/// Originally taken from MahApps.
14+
/// </remarks>
15+
public class CustomValidationPopup : Popup
1716
{
1817
private Window _hostWindow;
1918

2019
public CustomValidationPopup()
2120
{
22-
this.Loaded += this.CustomValidationPopup_Loaded;
23-
this.Opened += this.CustomValidationPopup_Opened;
21+
Loaded += OnLoaded;
22+
Unloaded += OnUnloaded;
2423
}
2524

2625
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
2726
{
28-
this.IsOpen = false;
27+
IsOpen = false;
2928
}
3029

31-
private void CustomValidationPopup_Loaded(object sender, RoutedEventArgs e)
30+
private void OnLoaded(object sender, RoutedEventArgs e)
3231
{
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;
6134

62-
private void CustomValidationPopup_Opened(object sender, EventArgs e)
63-
{
64-
//this.SetTopmostState(true);
65-
}
35+
target.SizeChanged += HostWindowSizeOrLocationChanged;
6636

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;
7139

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;
7544
}
7645

77-
private void CustomValidationPopup_Unloaded(object sender, RoutedEventArgs e)
46+
private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
7847
{
79-
var target = this.PlacementTarget as FrameworkElement;
48+
var target = PlacementTarget as FrameworkElement;
8049
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;
9557
}
9658

97-
private void hostWindow_StateChanged(object sender, EventArgs e)
59+
private void HostWindowOnStateChanged(object sender, EventArgs e)
9860
{
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);
11071
}
11172

112-
private void hostWindow_SizeOrLocationChanged(object sender, EventArgs e)
73+
private void HostWindowSizeOrLocationChanged(object sender, EventArgs e)
11374
{
114-
var offset = this.HorizontalOffset;
75+
var offset = HorizontalOffset;
11576
// "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;
11879
}
11980
}
12081
}

MaterialDesignThemes.Wpf/MaterialDesignThemes.Wpf.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@
203203
<Compile Include="ClockChoiceMadeEventArgs.cs" />
204204
<Compile Include="ClockItemButton.cs" />
205205
<Compile Include="ColorZone.cs" />
206-
<Compile Include="Converters\BooleanToVisibilityConverter.cs" />
207206
<Compile Include="Converters\BrushToRadialGradientBrushConverter.cs" />
208207
<Compile Include="Converters\CalendarDateCoalesceConverter.cs" />
209208
<Compile Include="Converters\CircularProgressBar\ArcEndPointConverter.cs" />

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.TextBox.xaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf"
44
xmlns:converters="clr-namespace:MaterialDesignThemes.Wpf.Converters">
55

6+
<ResourceDictionary.MergedDictionaries>
7+
<ResourceDictionary Source="MaterialDesignTheme.ValidationErrorTemplate.xaml" />
8+
</ResourceDictionary.MergedDictionaries>
9+
610
<converters:TextFieldHintVisibilityConverter x:Key="TextFieldHintVisibilityConverter" />
711

8-
9-
1012
<Style x:Key="MaterialDesignTextBox" TargetType="{x:Type TextBox}">
1113
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/>
1214
<Setter Property="BorderBrush" Value="{DynamicResource MaterialDesignTextBoxBorder}" />
@@ -21,7 +23,7 @@
2123
<Setter Property="AllowDrop" Value="true"/>
2224
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
2325
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
24-
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource MaterialDesignValidationErrorTemplate}"/>
26+
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource MaterialDesignValidationErrorTemplate}"/>
2527
<Setter Property="Template">
2628
<Setter.Value>
2729
<ControlTemplate TargetType="{x:Type TextBox}">

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.ValidationErrorTemplate.xaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3-
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf"
4-
xmlns:converters="clr-namespace:MaterialDesignThemes.Wpf.Converters">
5-
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" TrueValue="Visible" FalseValue="Collapsed"/>
3+
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf">
4+
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
65

76
<ControlTemplate x:Key="MaterialDesignValidationErrorTemplate">
87
<ControlTemplate.Resources>

0 commit comments

Comments
 (0)