-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowClosingBehavior.cs
127 lines (110 loc) · 3.85 KB
/
WindowClosingBehavior.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
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
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
/// <summary>
/// This module is included here because it can be used in all WPF projects to
/// manage the closing behavior of the main window; e.g., determing whether there
/// is a need to save before exiting, preventing exiting on some conditions, etc.
/// </summary>
namespace Common
{
public class WindowClosingBehavior
{
public static ICommand GetClosed(DependencyObject obj)
{
return (ICommand)obj.GetValue(ClosedProperty);
}
public static void SetClosed(DependencyObject obj, ICommand value)
{
obj.SetValue(ClosedProperty, value);
}
public static readonly DependencyProperty ClosedProperty
= DependencyProperty.RegisterAttached(
"Closed", typeof(ICommand), typeof(WindowClosingBehavior),
new UIPropertyMetadata(new PropertyChangedCallback(ClosedChanged)));
private static void ClosedChanged(
DependencyObject target, DependencyPropertyChangedEventArgs e)
{
Window window = target as Window;
if (window != null)
{
if (e.NewValue != null)
{
window.Closed += Window_Closed;
}
else
{
window.Closed -= Window_Closed;
}
}
}
public static ICommand GetClosing(DependencyObject obj)
{
return (ICommand)obj.GetValue(ClosingProperty);
}
public static void SetClosing(DependencyObject obj, ICommand value)
{
obj.SetValue(ClosingProperty, value);
}
public static readonly DependencyProperty ClosingProperty
= DependencyProperty.RegisterAttached(
"Closing", typeof(ICommand), typeof(WindowClosingBehavior),
new UIPropertyMetadata(new PropertyChangedCallback(ClosingChanged)));
private static void ClosingChanged(
DependencyObject target, DependencyPropertyChangedEventArgs e)
{
Window window = target as Window;
if (window != null)
{
if (e.NewValue != null)
{
window.Closing += Window_Closing;
}
else
{
window.Closing -= Window_Closing;
}
}
}
public static ICommand GetCancelClosing(DependencyObject obj)
{
return (ICommand)obj.GetValue(CancelClosingProperty);
}
public static void SetCancelClosing(DependencyObject obj, ICommand value)
{
obj.SetValue(CancelClosingProperty, value);
}
public static readonly DependencyProperty CancelClosingProperty
= DependencyProperty.RegisterAttached(
"CancelClosing", typeof(ICommand), typeof(WindowClosingBehavior));
static void Window_Closed(object sender, EventArgs e)
{
ICommand closed = GetClosed(sender as Window);
if (closed != null)
{
closed.Execute(null);
}
}
static void Window_Closing(object sender, CancelEventArgs e)
{
ICommand closing = GetClosing(sender as Window);
if (closing != null)
{
if (closing.CanExecute(null))
{
closing.Execute(null);
}
else
{
ICommand cancelClosing = GetCancelClosing(sender as Window);
if (cancelClosing != null)
{
cancelClosing.Execute(null);
}
e.Cancel = true;
}
}
}
}
}