This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
/
ModalHelper.cs
182 lines (164 loc) · 6.43 KB
/
ModalHelper.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using DevExpress.Xpf.Docking;
using DevExpress.Xpf.Docking.Base;
namespace Example
{
public class BaseLayoutItemState
{
public bool AllowActivate { get; set; }
public bool AllowClose { get; set; }
public bool AllowDock { get; set; }
public bool AllowDrag { get; set; }
public bool AllowDrop { get; set; }
public bool AllowFloat { get; set; }
public bool AllowHide { get; set; }
public bool AllowMinimize { get; set; }
public bool AllowMaximize { get; set; }
public bool AllowMove { get; set; }
public bool AllowRestore { get; set; }
public bool AllowSelection { get; set; }
public bool AllowSizing { get; set; }
public bool Focusable { get; set; }
public bool IsEnabled { get; set; }
}
public class ModalHelper
{
public static DependencyProperty IsModalProperty =
DependencyProperty.RegisterAttached("IsModal",
typeof(bool), typeof(ModalHelper),
new PropertyMetadata(false, new PropertyChangedCallback(IsModalChanged)));
public static DependencyProperty WindowStateProperty =
DependencyProperty.RegisterAttached("WindowState",
typeof(BaseLayoutItemState), typeof(ModalHelper));
public static bool GetIsModal(DependencyObject d)
{
return (bool)d.GetValue(IsModalProperty);
}
public static void SetIsModal(DependencyObject d, bool value)
{
d.SetValue(IsModalProperty, value);
}
private static void IsModalChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
BaseLayoutItem item = d as BaseLayoutItem;
if (d != null)
{
SetIsModal(item, (bool)e.NewValue);
if ((bool)e.NewValue)
{
CreateStates(item);
LockWindows(item);
item.GetDockLayoutManager().DockItemClosing += ModalHelper_DockItemClosing;
}
else
{
UnlockWindows(item);
item.GetDockLayoutManager().DockItemClosing -= ModalHelper_DockItemClosing;
}
}
}
static void ModalHelper_DockItemClosing(object sender, ItemCancelEventArgs e)
{
SetIsModal(e.Item, false);
}
public static BaseLayoutItemState GetWindowState(DependencyObject d)
{
return (BaseLayoutItemState)d.GetValue(WindowStateProperty);
}
public static void SetWindowState(DependencyObject d, BaseLayoutItemState value)
{
d.SetValue(WindowStateProperty, value);
}
private static void UnlockWindows(BaseLayoutItem window)
{
if (window.Parent != null)
{
foreach (var item in window.Parent.Items)
{
BaseLayoutItemState state = GetWindowState(item);
if (state != null)
{
item.AllowActivate = state.AllowActivate;
item.AllowClose = state.AllowClose;
item.AllowDock = state.AllowDock;
item.AllowDrag = state.AllowDrag;
item.AllowDrop = state.AllowDrop;
item.AllowFloat = state.AllowFloat;
item.AllowHide = state.AllowHide;
item.AllowMaximize = state.AllowMaximize;
item.AllowMinimize = state.AllowMinimize;
item.AllowMove = state.AllowMove;
item.AllowRestore = state.AllowRestore;
item.AllowSelection = state.AllowSelection;
item.AllowSizing = state.AllowSizing;
item.Focusable = state.Focusable;
item.IsEnabled = state.IsEnabled;
state = null;
}
}
}
}
private static void LockWindows(BaseLayoutItem window)
{
if (window.Parent != null)
{
foreach (var item in window.Parent.Items)
{
if (!GetIsModal(item))
{
item.AllowActivate = false;
item.AllowClose = false;
item.AllowDock = false;
item.AllowDrag = false;
item.AllowDrop = false;
item.AllowFloat = false;
item.AllowHide = false;
item.AllowMaximize = false;
item.AllowMinimize = false;
item.AllowMove = false;
item.AllowRestore = false;
item.AllowSelection = false;
item.AllowSizing = false;
item.Focusable = false;
item.IsEnabled = false;
}
}
}
}
private static void CreateWindowState(BaseLayoutItem window)
{
BaseLayoutItemState state = new BaseLayoutItemState()
{
AllowActivate = window.AllowActivate,
AllowClose = window.AllowClose,
AllowDock = window.AllowDock,
AllowDrag = window.AllowDrag,
AllowDrop = window.AllowDrop,
AllowFloat = window.AllowFloat,
AllowHide = window.AllowHide,
AllowMaximize = window.AllowMaximize,
AllowMinimize = window.AllowMinimize,
AllowMove = window.AllowMove,
AllowRestore = window.AllowRestore,
AllowSelection = window.AllowSelection,
AllowSizing = window.AllowSizing,
Focusable = window.Focusable,
IsEnabled = window.IsEnabled
};
SetWindowState(window, state);
}
private static void CreateStates(BaseLayoutItem window)
{
if (window.Parent != null)
{
foreach (var item in window.Parent.Items)
CreateWindowState(item);
}
}
}
}