forked from MahApps/MahApps.Metro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
605 lines (505 loc) · 25.3 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using MetroDemo.ExampleWindows;
namespace MetroDemo
{
public partial class MainWindow : MetroWindow
{
private bool shutdown;
private readonly MainWindowViewModel viewModel;
private FlyoutDemo? flyoutDemo;
public MainWindow()
{
this.viewModel = new MainWindowViewModel(DialogCoordinator.Instance);
this.DataContext = this.viewModel;
this.InitializeComponent();
DialogManager.DialogOpened += (_, args) => Debug.WriteLine($"Dialog {args.Dialog} - '{args.Dialog.Title}' opened.");
DialogManager.DialogClosed += (_, args) => Debug.WriteLine($"Dialog {args.Dialog} - '{args.Dialog.Title}' closed.");
}
#region DependencyProperties
public static readonly DependencyProperty ToggleFullScreenProperty =
DependencyProperty.Register(nameof(ToggleFullScreen),
typeof(bool),
typeof(MainWindow),
new PropertyMetadata(default(bool), OnToggleFullScreenChanged));
private static void OnToggleFullScreenChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != e.NewValue)
{
var window = (MainWindow)dependencyObject;
var fullScreen = (bool)e.NewValue;
if (fullScreen)
{
window.SetCurrentValue(IgnoreTaskbarOnMaximizeProperty, true);
window.SetCurrentValue(WindowStateProperty, WindowState.Maximized);
window.SetCurrentValue(WindowStyleProperty, WindowStyle.None);
window.SetCurrentValue(ShowTitleBarProperty, false);
}
else
{
window.SetCurrentValue(WindowStateProperty, WindowState.Normal);
window.SetCurrentValue(WindowStyleProperty, WindowStyle.SingleBorderWindow);
window.SetCurrentValue(ShowTitleBarProperty, true);
window.SetCurrentValue(IgnoreTaskbarOnMaximizeProperty, false);
}
}
}
public bool ToggleFullScreen
{
get => (bool)this.GetValue(ToggleFullScreenProperty);
set => this.SetValue(ToggleFullScreenProperty, value);
}
public static readonly DependencyProperty UseAccentForDialogsProperty =
DependencyProperty.Register(nameof(UseAccentForDialogs),
typeof(bool),
typeof(MainWindow),
new PropertyMetadata(default(bool), OnUseAccentForDialogsChanged));
private static void OnUseAccentForDialogsChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != e.NewValue)
{
var window = (MainWindow)dependencyObject;
var useAccentForDialogs = (bool)e.NewValue;
if (useAccentForDialogs == true && window.MetroDialogOptions!.ColorScheme == MetroDialogColorScheme.Inverted)
{
window.SetValue(UseInvertForDialogsProperty, false);
}
window.MetroDialogOptions!.ColorScheme = useAccentForDialogs ? MetroDialogColorScheme.Accented : MetroDialogColorScheme.Theme;
}
}
public bool UseAccentForDialogs
{
get => (bool)this.GetValue(UseAccentForDialogsProperty);
set => this.SetValue(UseAccentForDialogsProperty, value);
}
public static readonly DependencyProperty UseInvertForDialogsProperty =
DependencyProperty.Register(nameof(UseInvertForDialogs),
typeof(bool),
typeof(MainWindow),
new PropertyMetadata(default(bool), OnUseInvertForDialogsChanged));
private static void OnUseInvertForDialogsChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != e.NewValue)
{
var window = (MainWindow)dependencyObject;
var useInvertForDialogs = (bool)e.NewValue;
if (useInvertForDialogs == true && window.MetroDialogOptions!.ColorScheme == MetroDialogColorScheme.Accented)
{
window.SetValue(UseAccentForDialogsProperty, false);
}
window.MetroDialogOptions!.ColorScheme = useInvertForDialogs ? MetroDialogColorScheme.Inverted : MetroDialogColorScheme.Theme;
}
}
public bool UseInvertForDialogs
{
get => (bool)this.GetValue(UseInvertForDialogsProperty);
set => this.SetValue(UseInvertForDialogsProperty, value);
}
public static readonly DependencyProperty ShowIconOnDialogsProperty =
DependencyProperty.Register(nameof(ShowIconOnDialogs),
typeof(bool),
typeof(MainWindow),
new PropertyMetadata(default(bool), OnShowIconOnDialogsChanged));
private static void OnShowIconOnDialogsChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != e.NewValue)
{
var window = (MainWindow)dependencyObject;
var showIconOnDialogs = (bool)e.NewValue;
window.MetroDialogOptions!.Icon = showIconOnDialogs
? new MahApps.Metro.IconPacks.PackIconMaterial()
{
Kind = MahApps.Metro.IconPacks.PackIconMaterialKind.Duck,
Width = 75,
Height = 75,
Foreground = Brushes.Goldenrod,
}
: null;
}
}
public bool ShowIconOnDialogs
{
get => (bool)this.GetValue(ShowIconOnDialogsProperty);
set => this.SetValue(ShowIconOnDialogsProperty, value);
}
#endregion DependencyProperties
private void LaunchMahAppsOnGitHub(object sender, RoutedEventArgs e)
{
Process.Start("https://github.com/MahApps/MahApps.Metro");
}
private void LaunchSizeToContentDemo(object sender, RoutedEventArgs e)
{
new SizeToContentDemo { Owner = this }.Show();
}
private void LaunchVisualStudioDemo(object sender, RoutedEventArgs e)
{
new VSDemo().Show();
}
private void LaunchFlyoutDemo(object sender, RoutedEventArgs e)
{
if (this.flyoutDemo is null)
{
this.flyoutDemo = new FlyoutDemo();
this.flyoutDemo.Closed += (o, args) => this.flyoutDemo = null;
}
this.flyoutDemo.Launch();
}
private void LaunchIcons(object sender, RoutedEventArgs e)
{
Process.Start(new ProcessStartInfo
{
FileName = "https://github.com/MahApps/MahApps.Metro.IconPacks",
// UseShellExecute is default to false on .NET Core while true on .NET Framework.
// Only this value is set to true, the url link can be opened.
UseShellExecute = true,
});
}
private Window? cleanWindowDemo;
private void LauchCleanDemo(object sender, RoutedEventArgs e)
{
if (this.cleanWindowDemo == null)
{
this.cleanWindowDemo = new CleanWindowDemo();
this.cleanWindowDemo.Closed += (o, args) => this.cleanWindowDemo = null;
}
if (this.cleanWindowDemo.IsVisible)
{
this.cleanWindowDemo.Hide();
}
else
{
this.cleanWindowDemo.Show();
}
}
#region Show Dialogs
private async void ShowMessageDialog(object sender, RoutedEventArgs e)
{
// This demo runs on .Net 4.0, but we're using the Microsoft.Bcl.Async package so we have async/await support
// The package is only used by the demo and not a dependency of the library!
var settings = new MetroDialogSettings(this.MetroDialogOptions)
{
AffirmativeButtonText = "Hi",
NegativeButtonText = "Go away!",
FirstAuxiliaryButtonText = "Cancel",
DialogButtonFontSize = 20D
};
MessageDialogResult result = await this.ShowMessageAsync("Hello!",
"Welcome to the world of metro!",
MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary,
settings);
await this.ShowMessageAsync("Result", $"You said ({result}): {(result == MessageDialogResult.Affirmative ? settings.AffirmativeButtonText : result == MessageDialogResult.FirstAuxiliary ? settings.FirstAuxiliaryButtonText : settings.NegativeButtonText)}");
}
private async void ShowLimitedMessageDialog(object sender, RoutedEventArgs e)
{
var settings = new MetroDialogSettings(this.MetroDialogOptions)
{
AffirmativeButtonText = "Hi",
NegativeButtonText = "Go away!",
FirstAuxiliaryButtonText = "Cancel",
MaximumBodyHeight = 100
};
MessageDialogResult result = await this.ShowMessageAsync("Hello!",
"Welcome to the world of metro!" + string.Join(Environment.NewLine, "abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz"),
MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary,
settings);
await this.ShowMessageAsync("Result", $"You said ({result}): {(result == MessageDialogResult.Affirmative ? settings.AffirmativeButtonText : result == MessageDialogResult.FirstAuxiliary ? settings.FirstAuxiliaryButtonText : settings.NegativeButtonText)}");
}
private async void ShowCustomDialog(object sender, RoutedEventArgs e)
{
var dialog = new CustomDialog(this.MetroDialogOptions) { Content = this.Resources["CustomDialogTest"], Title = "This dialog allows arbitrary content." };
await this.ShowMetroDialogAsync(dialog);
var textBlock = dialog.FindChild<TextBlock>("MessageTextBlock");
textBlock!.Text = "A message box will appear in 3 seconds.";
await Task.Delay(3000);
await this.ShowMessageAsync("Secondary dialog", "This message is shown on top of another.", MessageDialogStyle.Affirmative, new MetroDialogSettings(this.MetroDialogOptions) { OwnerCanCloseWithDialog = true });
textBlock.Text = "The dialog will close in 2 seconds.";
await Task.Delay(2000);
await this.HideMetroDialogAsync(dialog);
}
private async void ShowAwaitCustomDialog(object sender, RoutedEventArgs e)
{
var tcs = new TaskCompletionSource<bool>();
var dialog = new CustomDialog(this.MetroDialogOptions) { Content = this.Resources["CustomCloseDialogTest"], Title = "Custom Dialog which is awaitable" };
dialog.Tag = tcs;
await this.ShowMetroDialogAsync(dialog);
await tcs.Task;
await this.HideMetroDialogAsync(dialog);
await this.ShowMessageAsync("Dialog gone", "The custom dialog is now closed.");
}
private async void ShowSecondCustomDialog(object sender, RoutedEventArgs e)
{
await this.ShowMessageAsync("Second Dialog", "The first custom dialog is now behind this dialog.");
}
private void CloseCustomDialog(object sender, RoutedEventArgs e)
{
var dialog = ((DependencyObject)sender).TryFindParent<BaseMetroDialog>()!;
var tcs = dialog.Tag as TaskCompletionSource<bool>;
tcs?.TrySetResult(true);
}
private async void ShowLoginDialogPasswordPreview(object sender, RoutedEventArgs e)
{
var result = await this.ShowLoginAsync("Authentication", "Enter your credentials", new LoginDialogSettings(this.MetroDialogOptions) { InitialUsername = "MahApps", EnablePasswordPreview = true });
if (result == null)
{
//User pressed cancel
}
else
{
await this.ShowMessageAsync("Authentication Information", $"Username: {result.Username}\nPassword: {result.Password}");
}
}
private async void ShowLoginDialogOnlyPassword(object sender, RoutedEventArgs e)
{
var result = await this.ShowLoginAsync("Authentication", "Enter your password", new LoginDialogSettings(this.MetroDialogOptions) { ShouldHideUsername = true });
if (result == null)
{
//User pressed cancel
}
else
{
await this.ShowMessageAsync("Authentication Information", $"Password: {result.Password}");
}
}
private async void ShowLoginDialogWithRememberCheckBox(object sender, RoutedEventArgs e)
{
var result = await this.ShowLoginAsync("Authentication", "Enter your password", new LoginDialogSettings(this.MetroDialogOptions) { RememberCheckBoxVisibility = Visibility.Visible });
if (result == null)
{
//User pressed cancel
}
else
{
await this.ShowMessageAsync("Authentication Information", $"Username: {result.Username}\nPassword: {result.Password}\nShouldRemember: {result.ShouldRemember}");
}
}
private async void ShowProgressDialog(object sender, RoutedEventArgs e)
{
var settings = new MetroDialogSettings(this.MetroDialogOptions)
{
NegativeButtonText = "Close now",
AnimateShow = false,
AnimateHide = false
};
var controller = await this.ShowProgressAsync("Please wait...", "We are baking now some cupcakes!", settings: settings);
controller.SetIndeterminate();
await Task.Delay(3000);
controller.SetCancelable(true);
double i = 0.0;
while (i < 6.0)
{
if (controller.IsCanceled)
{
break;
}
var val = (i / 100.0) * 20.0;
controller.SetProgress(val);
controller.SetMessage("Baking cupcake: " + i + "...");
i += 1.0;
await Task.Delay(2000);
}
await controller.CloseAsync();
if (controller.IsCanceled)
{
await this.ShowMessageAsync("No cupcakes!", "You stopped baking!");
}
else
{
await this.ShowMessageAsync("Cupcakes!", "Your cupcakes are finished! Enjoy!");
}
}
private async void ShowInputDialog(object sender, RoutedEventArgs e)
{
string? result = await this.ShowInputAsync("Hello!", "What is your name?");
if (string.IsNullOrWhiteSpace(result)) //user pressed cancel
{
return;
}
await this.ShowMessageAsync("Hello", "Hello " + result + "!");
}
private async void ShowInputDialogCustomButtonSizes(object sender, RoutedEventArgs e)
{
var settings = new MetroDialogSettings(this.MetroDialogOptions)
{
DialogButtonFontSize = 24D
};
var result = await this.ShowInputAsync("Hello!", "What is your name?", settings);
if (result == null) //user pressed cancel
{
return;
}
await this.ShowMessageAsync("Hello", "Hello " + result + "!");
}
private async void ShowLoginDialog(object sender, RoutedEventArgs e)
{
var result = await this.ShowLoginAsync("Authentication", "Enter your credentials", new LoginDialogSettings(this.MetroDialogOptions) { InitialUsername = "MahApps" });
if (result == null)
{
//User pressed cancel
}
else
{
await this.ShowMessageAsync("Authentication Information", $"Username: {result.Username}\nPassword: {result.Password}");
}
}
#endregion
#region Show Dialog Outside
private void ShowInputDialogOutside(object sender, RoutedEventArgs e)
{
var result = this.ShowModalInputExternal("Hello!", "What is your name?", new MetroDialogSettings(this.MetroDialogOptions) { AnimateShow = false });
if (result == null) //user pressed cancel
{
return;
}
this.ShowModalMessageExternal("Hello", "Hello " + result + "!");
}
private void ShowLoginDialogOutside(object sender, RoutedEventArgs e)
{
var result = this.ShowModalLoginExternal("Authentication", "Enter your credentials", new LoginDialogSettings(this.MetroDialogOptions) { InitialUsername = "MahApps", EnablePasswordPreview = true });
if (result == null)
{
//User pressed cancel
}
else
{
MessageDialogResult messageResult = this.ShowModalMessageExternal("Authentication Information", $"Username: {result.Username}\nPassword: {result.Password}");
}
}
private void ShowMessageDialogOutside(object sender, RoutedEventArgs e)
{
var settings = new MetroDialogSettings(this.MetroDialogOptions)
{
AffirmativeButtonText = "Hi",
NegativeButtonText = "Go away!",
FirstAuxiliaryButtonText = "Cancel",
ColorScheme = this.MetroDialogOptions!.ColorScheme
};
MessageDialogResult result = this.ShowModalMessageExternal("Hello!", "Welcome to the world of metro!",
MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary, settings);
if (result != MessageDialogResult.FirstAuxiliary)
{
this.ShowModalMessageExternal("Result", "You said: " + (result == MessageDialogResult.Affirmative
? settings.AffirmativeButtonText
: settings.NegativeButtonText +
Environment.NewLine + Environment.NewLine + "This dialog will follow the Use Accent setting."));
}
}
#endregion
private void InteropDemo(object sender, RoutedEventArgs e)
{
new InteropDemo().Show();
}
private void LaunchNavigationDemo(object sender, RoutedEventArgs e)
{
var navWin = new MetroNavigationWindow
{
Title = "Navigation Demo",
Width = 800,
Height = 600,
ShowHomeButton = true
};
//uncomment the next two lines if you want the clean style.
//navWin.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Clean/MetroWindow.xaml", UriKind.Absolute) });
//navWin.SetResourceReference(StyleProperty, "MahApps.Styles.MetroWindow.Clean");
navWin.Show();
navWin.Navigate(new Navigation.HomePage());
}
private void MetroWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (e.Cancel)
{
return;
}
if (this.viewModel.QuitConfirmationEnabled
&& this.shutdown == false)
{
e.Cancel = true;
// We have to delay the execution through BeginInvoke to prevent potential re-entrancy
this.Dispatcher.BeginInvoke(new Action(async () => await this.ConfirmShutdown()));
}
else
{
this.flyoutDemo?.Dispose();
this.viewModel.Dispose();
}
}
private async Task ConfirmShutdown()
{
var mySettings = new MetroDialogSettings
{
AffirmativeButtonText = "Quit",
NegativeButtonText = "Cancel",
AnimateShow = true,
AnimateHide = false
};
var result = await this.ShowMessageAsync("Quit application?",
"Sure you want to quit application?",
MessageDialogStyle.AffirmativeAndNegative,
mySettings);
this.shutdown = result == MessageDialogResult.Affirmative;
if (this.shutdown)
{
Application.Current.Shutdown();
}
}
private MetroWindow? testWindow;
private MetroWindow GetTestWindow()
{
this.testWindow?.Close();
this.testWindow = new MetroWindow
{
Owner = this,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Title = "A Window Test...",
Width = 500,
Height = 300,
ResizeMode = ResizeMode.CanResizeWithGrip
};
this.testWindow.Closed += (_, _) => this.testWindow = null;
return this.testWindow;
}
private void MenuWindowWithBorderOnClick(object sender, RoutedEventArgs e)
{
var w = this.GetTestWindow();
w.Content = new TextBlock { Text = "MetroWindow with Border", FontSize = 28, FontWeight = FontWeights.Light, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center };
w.BorderThickness = new Thickness(1);
w.Show();
}
private void MenuWindowWithRoundedBorderOnClick(object sender, RoutedEventArgs e)
{
var w = this.GetTestWindow();
w.Content = new TextBlock { Text = "MetroWindow with rounded Border", FontSize = 28, FontWeight = FontWeights.Light, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center };
w.GlowColor = null;
w.NonActiveGlowColor = null;
w.BorderThickness = new Thickness(1);
w.WindowStyle = WindowStyle.None;
w.AllowsTransparency = true;
ControlsHelper.SetCornerRadius(w, new CornerRadius(8));
w.Show();
}
private void MenuWindowWithGlowOnClick(object sender, RoutedEventArgs e)
{
var w = this.GetTestWindow();
w.Content = new Button { Content = "MetroWindow with Glow", ToolTip = "This is a tool tip", FontSize = 28, FontWeight = FontWeights.Light, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center };
w.Show();
}
private void MenuWindowWithoutGlowOnClick(object sender, RoutedEventArgs e)
{
var w = this.GetTestWindow();
w.Content = new TextBlock { Text = "MetroWindow without Glow", FontSize = 28, FontWeight = FontWeights.Light, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center };
w.GlowColor = null;
w.NonActiveGlowColor = null;
w.BorderThickness = new Thickness(1);
w.WindowStyle = WindowStyle.None;
w.Show();
}
}
}