forked from Dirkster99/AvalonDock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
266 lines (222 loc) · 8.55 KB
/
MainWindow.xaml.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/************************************************************************
AvalonDock
Copyright (C) 2007-2013 Xceed Software Inc.
This program is provided to you under the terms of the Microsoft Public
License (Ms-PL) as published at https://opensource.org/licenses/MS-PL
************************************************************************/
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using AvalonDock.Layout;
using System.Diagnostics;
using System.IO;
using AvalonDock.Layout.Serialization;
using AvalonDock;
using System.Diagnostics.CodeAnalysis;
namespace TestApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
Random rnd = new Random();
timer.Interval = TimeSpan.FromSeconds(1.0);
timer.Tick += (s, e) =>
{
TestTimer++;
TestBackground = new SolidColorBrush(Color.FromRgb(
(byte)rnd.Next(0, 255), (byte)rnd.Next(0, 255), (byte)rnd.Next(0, 255)));
FocusedElement = Keyboard.FocusedElement == null ? string.Empty : Keyboard.FocusedElement.ToString();
//Debug.WriteLine(string.Format("ActiveContent = {0}", dockManager.ActiveContent));
};
timer.Start();
this.DataContext = this;
winFormsHost.Child = new UserControl1();
}
#region TestTimer
/// <summary>
/// TestTimer Dependency Property
/// </summary>
public static readonly DependencyProperty TestTimerProperty =
DependencyProperty.Register("TestTimer", typeof(int), typeof(MainWindow),
new FrameworkPropertyMetadata((int)0));
/// <summary>
/// Gets or sets the TestTimer property. This dependency property
/// indicates a test timer that elapses evry one second (just for binding test).
/// </summary>
public int TestTimer
{
get => (int)GetValue(TestTimerProperty);
set => SetValue(TestTimerProperty, value);
}
#endregion
#region TestBackground
/// <summary>
/// TestBackground Dependency Property
/// </summary>
public static readonly DependencyProperty TestBackgroundProperty =
DependencyProperty.Register("TestBackground", typeof(Brush), typeof(MainWindow),
new FrameworkPropertyMetadata((Brush)null));
/// <summary>
/// Gets or sets the TestBackground property. This dependency property
/// indicates a randomly changing brush (just for testing).
/// </summary>
public Brush TestBackground
{
get => (Brush)GetValue(TestBackgroundProperty);
set => SetValue(TestBackgroundProperty, value);
}
#endregion
#region FocusedElement
/// <summary>
/// FocusedElement Dependency Property
/// </summary>
public static readonly DependencyProperty FocusedElementProperty =
DependencyProperty.Register("FocusedElement", typeof(string), typeof(MainWindow),
new FrameworkPropertyMetadata((IInputElement)null));
/// <summary>
/// Gets or sets the FocusedElement property. This dependency property
/// indicates ....
/// </summary>
public string FocusedElement
{
get => (string)GetValue(FocusedElementProperty);
set => SetValue(FocusedElementProperty, value);
}
#endregion
private void OnLayoutRootPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
var activeContent = ((LayoutRoot)sender).ActiveContent;
if (e.PropertyName == "ActiveContent")
{
Debug.WriteLine(string.Format("ActiveContent-> {0}", activeContent));
}
}
[SuppressMessage("Style", "IDE0063:使用简单的 \"using\" 语句", Justification = "<挂起>")]
private void OnLoadLayout(object sender, RoutedEventArgs e)
{
var currentContentsList = dockManager.Layout.Descendents().OfType<LayoutContent>().Where(c => c.ContentId != null).ToArray();
string fileName = (sender as MenuItem).Header.ToString();
var serializer = new XmlLayoutSerializer(dockManager);
//serializer.LayoutSerializationCallback += (s, args) =>
// {
// var prevContent = currentContentsList.FirstOrDefault(c => c.ContentId == args.Model.ContentId);
// if (prevContent != null)
// args.Content = prevContent.Content;
// };
using (var stream = new StreamReader(string.Format(@".\AvalonDock_{0}.config", fileName)))
serializer.Deserialize(stream);
}
[SuppressMessage("Style", "IDE0063:使用简单的 \"using\" 语句", Justification = "<挂起>")]
private void OnSaveLayout(object sender, RoutedEventArgs e)
{
string fileName = (sender as MenuItem).Header.ToString();
var serializer = new XmlLayoutSerializer(dockManager);
using (var stream = new StreamWriter(string.Format(@".\AvalonDock_{0}.config", fileName)))
serializer.Serialize(stream);
}
private void OnShowWinformsWindow(object sender, RoutedEventArgs e)
{
var winFormsWindow = dockManager.Layout.Descendents().OfType<LayoutAnchorable>().Single(a => a.ContentId == "WinFormsWindow");
if (winFormsWindow.IsHidden)
winFormsWindow.Show();
else if (winFormsWindow.IsVisible)
winFormsWindow.IsActive = true;
else
winFormsWindow.AddToLayout(dockManager, AnchorableShowStrategy.Bottom | AnchorableShowStrategy.Most);
}
private void AddTwoDocuments_click(object sender, RoutedEventArgs e)
{
var firstDocumentPane = dockManager.Layout.Descendents().OfType<LayoutDocumentPane>().FirstOrDefault();
if (firstDocumentPane != null)
{
LayoutDocument doc = new LayoutDocument
{
Title = "Test1"
};
firstDocumentPane.Children.Add(doc);
LayoutDocument doc2 = new LayoutDocument
{
Title = "Test2"
};
firstDocumentPane.Children.Add(doc2);
}
var leftAnchorGroup = dockManager.Layout.LeftSide.Children.FirstOrDefault();
if (leftAnchorGroup == null)
{
leftAnchorGroup = new LayoutAnchorGroup();
dockManager.Layout.LeftSide.Children.Add(leftAnchorGroup);
}
leftAnchorGroup.Children.Add(new LayoutAnchorable() { Title = "New Anchorable" });
}
private void OnShowToolWindow1(object sender, RoutedEventArgs e)
{
var toolWindow1 = dockManager.Layout.Descendents().OfType<LayoutAnchorable>().Single(a => a.ContentId == "toolWindow1");
if (toolWindow1.IsHidden)
toolWindow1.Show();
else if (toolWindow1.IsVisible)
toolWindow1.IsActive = true;
else
toolWindow1.AddToLayout(dockManager, AnchorableShowStrategy.Bottom | AnchorableShowStrategy.Most);
}
private void DockManager_DocumentClosing(object sender, DocumentClosingEventArgs e)
{
if (MessageBox.Show("Are you sure you want to close the document?", "AvalonDock Sample", MessageBoxButton.YesNo) == MessageBoxResult.No)
e.Cancel = true;
}
private void OnDumpToConsole(object sender, RoutedEventArgs e)
{
// Uncomment when TRACE is activated on AvalonDock project
// dockManager.Layout.ConsoleDump(0);
}
private void OnReloadManager(object sender, RoutedEventArgs e)
{
}
private void OnUnloadManager(object sender, RoutedEventArgs e)
{
if (layoutRoot.Children.Contains(dockManager))
layoutRoot.Children.Remove(dockManager);
}
private void OnLoadManager(object sender, RoutedEventArgs e)
{
if (!layoutRoot.Children.Contains(dockManager))
layoutRoot.Children.Add(dockManager);
}
private void OnToolWindow1Hiding(object sender, System.ComponentModel.CancelEventArgs e)
{
if (MessageBox.Show("Are you sure you want to hide this tool?", "AvalonDock", MessageBoxButton.YesNo) == MessageBoxResult.No)
e.Cancel = true;
}
private void OnShowHeader(object sender, RoutedEventArgs e)
{
//// LayoutDocumentPane.ShowHeader = !LayoutDocumentPane.ShowHeader;
}
/// <summary>
/// Method create a new anchorable window to test whether a floating window will auto-adjust its size to the
/// containing control. See <see cref="DockingManager.AutoWindowSizeWhenOpened"/> dependency property.
/// and TestUserControl in this demo App for more details.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnNewFloatingWindow(object sender, RoutedEventArgs e)
{
var view = new TestUserControl();
var anchorable = new LayoutAnchorable()
{
Title = "Floating window with initial usercontrol size",
Content = view
};
anchorable.AddToLayout(dockManager,AnchorableShowStrategy.Most);
anchorable.Float();
}
}
}