-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathExtendedDialog.cs
More file actions
140 lines (119 loc) · 4.01 KB
/
ExtendedDialog.cs
File metadata and controls
140 lines (119 loc) · 4.01 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
namespace Menees.Windows.Presentation
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
#endregion
/// <summary>
/// The base class for Menees WPF dialog windows.
/// </summary>
public class ExtendedDialog : ExtendedWindow
{
#region Public Fields
/// <summary>
/// The DialogResult dependency property.
/// </summary>
public static readonly DependencyProperty DialogResultProperty = DependencyProperty.RegisterAttached(
"DialogResult",
typeof(bool?),
typeof(ExtendedDialog),
new UIPropertyMetadata
{
// Idea came from: http://stackoverflow.com/questions/1759372/where-is-button-dialogresult-in-wpf/1759505#1759505
PropertyChangedCallback = (obj, e) =>
{
if (obj is not Button button)
{
throw Exceptions.NewInvalidOperationException("DialogResult can only be used on a Button control.");
}
button.Click += (sender, e2) =>
{
GetWindow(button).DialogResult = GetDialogResult(button);
};
},
});
/// <summary>
/// The ShowIcon dependency property.
/// </summary>
public static readonly DependencyProperty ShowIconProperty = DependencyProperty.Register(
"ShowIcon",
typeof(bool),
typeof(ExtendedDialog),
new FrameworkPropertyMetadata(
false,
new PropertyChangedCallback((obj, e) =>
{
if (obj is not ExtendedDialog dialog)
{
throw Exceptions.NewInvalidOperationException("ShowIcon can only be used on an ExtendedDialog.");
}
})));
#endregion
#region Constructors
static ExtendedDialog()
{
// Tell WPF that we're changing some default property values from their Window settings to better values for modal dialogs.
ResizeModeProperty.OverrideMetadata(typeof(ExtendedDialog), new FrameworkPropertyMetadata(ResizeMode.NoResize));
ShowInTaskbarProperty.OverrideMetadata(typeof(ExtendedDialog), new FrameworkPropertyMetadata(false));
}
/// <summary>
/// Creates a new instance.
/// </summary>
public ExtendedDialog()
{
// WindowStartupLocation isn't a dependency property, so have to change its default from the instance constructor.
this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
}
#endregion
#region Public Methods
/// <summary>
/// Gets the DialogResult dependency property value for the specified button.
/// </summary>
public static bool? GetDialogResult(Button button) => (bool?)button.GetValue(DialogResultProperty);
/// <summary>
/// Sets the DialogResult dependency property value for the specified button.
/// </summary>
public static void SetDialogResult(Button button, bool? value)
{
button.SetValue(DialogResultProperty, value);
}
/// <summary>
/// Gets the ShowIcon dependency property value for the dialog.
/// </summary>
public static bool GetShowIcon(ExtendedDialog dialog) => (bool)dialog.GetValue(ShowIconProperty);
/// <summary>
/// Sets the ShowIcon dependency property value for the dialog.
/// </summary>
public static void SetShowIcon(ExtendedDialog dialog, bool value)
{
dialog.SetValue(ShowIconProperty, value);
}
#endregion
#region Protected Methods
/// <summary>
/// Applies the ShowIcon dependency property.
/// </summary>
/// <param name="e"></param>
protected override void OnSourceInitialized(EventArgs e)
{
// Idea(s) came from:
// http://stackoverflow.com/questions/3096359/wpf-remove-system-menu-icon-from-modal-window-but-not-main-app-window?rq=1
// http://stackoverflow.com/questions/18580430/hide-the-icon-from-a-wpf-window
// http://stackoverflow.com/questions/2341230/removing-icon-from-a-wpf-window
// http://www.wpftutorial.net/RemoveIcon.html
// WPF will show the icon by default, so we're ok as long as this property isn't toggled at run-time.
if (!GetShowIcon(this))
{
NativeMethods.RemoveIcon(this);
}
base.OnSourceInitialized(e);
}
#endregion
}
}