Mercurial > silverbladetech
comparison Chronosv2/source/Presentation/Windows/Controls/WindowElement.cs @ 10:443821e55f06
Initial cleaned up add from Codeplex files
author | stevenh7776 stevenhollidge@hotmail.com |
---|---|
date | Tue, 21 Feb 2012 17:25:44 +0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:904a9faadf8b | 10:443821e55f06 |
---|---|
1 /* | |
2 The MIT License | |
3 | |
4 Copyright (c) 2009-2010. Carlos Guzmán Álvarez. http://chronoswpf.codeplex.com/ | |
5 | |
6 Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 of this software and associated documentation files (the "Software"), to deal | |
8 in the Software without restriction, including without limitation the rights | |
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 copies of the Software, and to permit persons to whom the Software is | |
11 furnished to do so, subject to the following conditions: | |
12 | |
13 The above copyright notice and this permission notice shall be included in | |
14 all copies or substantial portions of the Software. | |
15 | |
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 THE SOFTWARE. | |
23 */ | |
24 | |
25 using System; | |
26 using System.ComponentModel; | |
27 using System.Diagnostics; | |
28 using System.Windows; | |
29 using System.Windows.Controls; | |
30 using System.Windows.Controls.Primitives; | |
31 using System.Windows.Data; | |
32 using System.Windows.Input; | |
33 using System.Windows.Interop; | |
34 using System.Windows.Media; | |
35 using System.Windows.Threading; | |
36 using Chronos.Extensions.Windows; | |
37 using Chronos.Presentation.Core.Windows; | |
38 using nRoute.Components; | |
39 | |
40 namespace Chronos.Presentation.Windows.Controls | |
41 { | |
42 /// <summary> | |
43 /// Provides the ability to create, configure, show, and manage the lifetime of windows | |
44 /// </summary> | |
45 [TemplatePart(Name = WindowElement.PART_CloseButton, Type = typeof(ButtonBase))] | |
46 [TemplatePart(Name = WindowElement.PART_ContentPresenter, Type = typeof(ContentPresenter))] | |
47 [TemplateVisualState(Name = NormalVisualState, GroupName = WindowStateGroup)] | |
48 [TemplateVisualState(Name = MinimizedVisualState, GroupName = WindowStateGroup)] | |
49 [TemplateVisualState(Name = MaximizedVisualState, GroupName = WindowStateGroup)] | |
50 public class WindowElement | |
51 : DesktopElement, IWindow, IModalVindow | |
52 { | |
53 #region · Constants · | |
54 | |
55 #region · Template Parts · | |
56 | |
57 private const string PART_ContentPresenter = "PART_ContentPresenter"; | |
58 private const string PART_Root = "PART_Root"; | |
59 private const string PART_MaximizeButton = "PART_MaximizeButton"; | |
60 private const string PART_MinimizeButton = "PART_MinimizeButton"; | |
61 private const string PART_CloseButton = "PART_CloseButton"; | |
62 | |
63 #endregion | |
64 | |
65 #region · Visual States · | |
66 | |
67 private const string WindowStateGroup = "WindowState"; | |
68 private const string NormalVisualState = "Normal"; | |
69 private const string MinimizedVisualState = "Minimized"; | |
70 private const string MaximizedVisualState = "Maximized"; | |
71 | |
72 #endregion | |
73 | |
74 #region · Misc · | |
75 | |
76 private const int MaximizeMargin = 20; | |
77 | |
78 #endregion | |
79 | |
80 #endregion | |
81 | |
82 #region · Dependency Properties · | |
83 | |
84 /// <summary> | |
85 /// Identifies the Title dependency property. | |
86 /// </summary> | |
87 public static readonly DependencyProperty TitleProperty = | |
88 DependencyProperty.Register("Title", typeof(String), typeof(WindowElement), | |
89 new FrameworkPropertyMetadata(String.Empty)); | |
90 | |
91 /// <summary> | |
92 /// Identifies the WindowState dependency property. | |
93 /// </summary> | |
94 public static readonly DependencyProperty WindowStateProperty = | |
95 DependencyProperty.Register("WindowState", typeof(WindowState), typeof(WindowElement), | |
96 new FrameworkPropertyMetadata(WindowState.Normal)); | |
97 | |
98 /// <summary> | |
99 /// Identifies the ShowCloseButton dependency property. | |
100 /// </summary> | |
101 public static readonly DependencyProperty ShowCloseButtonProperty = | |
102 DependencyProperty.Register("ShowCloseButton", typeof(bool), typeof(WindowElement), | |
103 new FrameworkPropertyMetadata(true)); | |
104 | |
105 /// <summary> | |
106 /// Identifies the ShowMaximizeButton dependency property. | |
107 /// </summary> | |
108 public static readonly DependencyProperty ShowMaximizeButtonProperty = | |
109 DependencyProperty.Register("ShowMaximizeButton", typeof(bool), typeof(WindowElement), | |
110 new FrameworkPropertyMetadata(true)); | |
111 | |
112 /// <summary> | |
113 /// Identifies the ShowMinimizeButton dependency property. | |
114 /// </summary> | |
115 public static readonly DependencyProperty ShowMinimizeButtonProperty = | |
116 DependencyProperty.Register("ShowMinimizeButton", typeof(bool), typeof(WindowElement), | |
117 new FrameworkPropertyMetadata(true)); | |
118 | |
119 /// <summary> | |
120 /// Identifies the DialogResult dependency property. | |
121 /// </summary> | |
122 public static readonly DependencyProperty DialogResultProperty = | |
123 DependencyProperty.Register("DialogResult", typeof(DialogResult), typeof(WindowElement), | |
124 new FrameworkPropertyMetadata(DialogResult.None)); | |
125 | |
126 /// <summary> | |
127 /// Identifies the ViewMode dependency property. | |
128 /// </summary> | |
129 public static readonly DependencyProperty ViewModeProperty = | |
130 DependencyProperty.Register("ViewMode", typeof(ViewModeType), typeof(WindowElement), | |
131 new FrameworkPropertyMetadata(ViewModeType.Default, | |
132 new PropertyChangedCallback(OnViewModeChanged))); | |
133 | |
134 #endregion | |
135 | |
136 #region · Dependency Properties Callback Handlers · | |
137 | |
138 private static void OnViewModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | |
139 { | |
140 if (d != null) | |
141 { | |
142 WindowElement window = d as WindowElement; | |
143 | |
144 if (window.IsActive) | |
145 { | |
146 ViewModeType oldViewModel = (ViewModeType)e.OldValue; | |
147 | |
148 if (oldViewModel == ViewModeType.Add | |
149 || oldViewModel == ViewModeType.Edit) | |
150 { | |
151 window.UpdateActiveElementBindings(); | |
152 } | |
153 else | |
154 { | |
155 window.MoveFocus(FocusNavigationDirection.Next); | |
156 } | |
157 } | |
158 } | |
159 } | |
160 | |
161 #endregion | |
162 | |
163 #region · Static members · | |
164 | |
165 /// <summary> | |
166 /// Container panel for modal windows | |
167 /// </summary> | |
168 public static Panel ModalContainerPanel; | |
169 | |
170 #endregion | |
171 | |
172 #region · Static Constructor · | |
173 | |
174 /// <summary> | |
175 /// Initializes the <see cref="WindowElement"/> class. | |
176 /// </summary> | |
177 static WindowElement() | |
178 { | |
179 WindowElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowElement), | |
180 new FrameworkPropertyMetadata(typeof(WindowElement))); | |
181 | |
182 KeyboardNavigation.IsTabStopProperty.OverrideMetadata(typeof(WindowElement), | |
183 new FrameworkPropertyMetadata(false)); | |
184 | |
185 KeyboardNavigation.DirectionalNavigationProperty.OverrideMetadata( | |
186 typeof(WindowElement), new FrameworkPropertyMetadata(KeyboardNavigationMode.Local)); | |
187 | |
188 KeyboardNavigation.TabNavigationProperty.OverrideMetadata( | |
189 typeof(WindowElement), new FrameworkPropertyMetadata(KeyboardNavigationMode.Cycle)); | |
190 | |
191 KeyboardNavigation.ControlTabNavigationProperty.OverrideMetadata( | |
192 typeof(WindowElement), new FrameworkPropertyMetadata(KeyboardNavigationMode.Once)); | |
193 | |
194 if (!DesignMode.IsInDesignMode | |
195 && Application.Current.GetRenderTier() != RenderTier.Tier2) | |
196 { | |
197 WindowElement.CacheModeProperty.OverrideMetadata(typeof(WindowElement), | |
198 new FrameworkPropertyMetadata(new BitmapCache { EnableClearType = true, RenderAtScale = 1, SnapsToDevicePixels = true })); | |
199 } | |
200 } | |
201 | |
202 #endregion | |
203 | |
204 #region · Events · | |
205 | |
206 /// <summary> | |
207 /// Occurs when the window is about to close. | |
208 /// </summary> | |
209 public event EventHandler Closed; | |
210 | |
211 /// <summary> | |
212 /// Occurs directly after System.Windows.Window.Close() is called, and can be | |
213 /// handled to cancel window closure. | |
214 /// </summary> | |
215 public event CancelEventHandler Closing; | |
216 | |
217 /// <summary> | |
218 /// Occurs when the window's System.Windows.Window.WindowState property changes. | |
219 /// </summary> | |
220 public event EventHandler WindowStateChanged; | |
221 | |
222 #endregion | |
223 | |
224 #region · Fields · | |
225 | |
226 private ContentPresenter contentPresenter; | |
227 private DispatcherFrame dispatcherFrame; | |
228 private WindowState oldWindowState; | |
229 private bool isShowed; | |
230 private bool isModal; | |
231 | |
232 #region · Commands · | |
233 | |
234 private ICommand minimizeCommand; | |
235 private ICommand maximizeCommand; | |
236 private ICommand closeCommand; | |
237 | |
238 #endregion | |
239 | |
240 #endregion | |
241 | |
242 #region · IWindow Commands · | |
243 | |
244 /// <summary> | |
245 /// Gets the maximize window command | |
246 /// </summary> | |
247 /// <value></value> | |
248 public ICommand MaximizeCommand | |
249 { | |
250 get | |
251 { | |
252 if (this.maximizeCommand == null) | |
253 { | |
254 this.maximizeCommand = new ActionCommand(() => OnMaximizeWindow()); | |
255 } | |
256 | |
257 return this.maximizeCommand; | |
258 } | |
259 } | |
260 | |
261 /// <summary> | |
262 /// Gets the minimize window command | |
263 /// </summary> | |
264 /// <value></value> | |
265 public ICommand MinimizeCommand | |
266 { | |
267 get | |
268 { | |
269 if (this.minimizeCommand == null) | |
270 { | |
271 this.minimizeCommand = new ActionCommand(() => OnMinimizeWindow()); | |
272 } | |
273 | |
274 return this.minimizeCommand; | |
275 } | |
276 } | |
277 | |
278 /// <summary> | |
279 /// Gets the close window command | |
280 /// </summary> | |
281 /// <value></value> | |
282 public ICommand CloseCommand | |
283 { | |
284 get | |
285 { | |
286 if (this.closeCommand == null) | |
287 { | |
288 this.closeCommand = new ActionCommand(() => OnCloseWindow()); | |
289 } | |
290 | |
291 return this.closeCommand; | |
292 } | |
293 } | |
294 | |
295 #endregion | |
296 | |
297 #region · Properties · | |
298 | |
299 /// <summary> | |
300 /// Gets or sets a window's title. This is a dependency property. | |
301 /// </summary> | |
302 public String Title | |
303 { | |
304 get { return (String)base.GetValue(WindowElement.TitleProperty); } | |
305 set { base.SetValue(WindowElement.TitleProperty, value); } | |
306 } | |
307 | |
308 /// <summary> | |
309 /// Gets the dialog result | |
310 /// </summary> | |
311 public DialogResult DialogResult | |
312 { | |
313 get { return (DialogResult)base.GetValue(DialogResultProperty); } | |
314 set { base.SetValue(DialogResultProperty, value); } | |
315 } | |
316 | |
317 /// <summary> | |
318 /// Gets or sets a value that indicates whether a window is restored, minimized, or maximized. | |
319 /// This is a dependency property. | |
320 /// </summary> | |
321 /// <value>A <see cref="WindowState"/> that determines whether a window is restored, minimized, or maximized. The default is Normal (restored).</value> | |
322 public WindowState WindowState | |
323 { | |
324 get { return (WindowState)base.GetValue(WindowStateProperty); } | |
325 set | |
326 { | |
327 if ((WindowState)this.GetValue(WindowStateProperty) != value) | |
328 { | |
329 if (this.oldWindowState == System.Windows.WindowState.Maximized | |
330 && this.WindowState == System.Windows.WindowState.Minimized | |
331 && value == System.Windows.WindowState.Normal) | |
332 { | |
333 this.UpdateWindowState(this.WindowState, this.oldWindowState); | |
334 | |
335 base.SetValue(WindowStateProperty, this.oldWindowState); | |
336 } | |
337 else | |
338 { | |
339 this.UpdateWindowState(this.WindowState, value); | |
340 | |
341 base.SetValue(WindowStateProperty, value); | |
342 } | |
343 | |
344 if (this.WindowStateChanged != null) | |
345 { | |
346 this.WindowStateChanged(this, EventArgs.Empty); | |
347 } | |
348 } | |
349 } | |
350 } | |
351 | |
352 /// <summary> | |
353 /// Gets a value that indicates whether the close button is visible. | |
354 /// This is a dependency property. | |
355 /// </summary> | |
356 public bool ShowCloseButton | |
357 { | |
358 get { return (bool)base.GetValue(ShowCloseButtonProperty); } | |
359 set { base.SetValue(ShowCloseButtonProperty, value); } | |
360 } | |
361 | |
362 /// <summary> | |
363 /// Gets a value that indicates whether the maximize button is visible. | |
364 /// This is a dependency property. | |
365 /// </summary> | |
366 public bool ShowMaximizeButton | |
367 { | |
368 get { return (bool)base.GetValue(ShowMaximizeButtonProperty); } | |
369 set { base.SetValue(ShowMaximizeButtonProperty, value); } | |
370 } | |
371 | |
372 /// <summary> | |
373 /// Gets a value that indicates whether the minimize button is visible. | |
374 /// This is a dependency property. | |
375 /// </summary> | |
376 public bool ShowMinimizeButton | |
377 { | |
378 get { return (bool)base.GetValue(ShowMinimizeButtonProperty); } | |
379 set { base.SetValue(ShowMinimizeButtonProperty, value); } | |
380 } | |
381 | |
382 /// <summary> | |
383 /// Gets a value indicating whether the element can be dragged. | |
384 /// </summary> | |
385 /// <value><c>true</c> if this instance can drag; otherwise, <c>false</c>.</value> | |
386 public override bool CanDrag | |
387 { | |
388 get { return (bool)base.GetValue(CanDragProperty) && this.WindowState == System.Windows.WindowState.Normal; } | |
389 set { base.SetValue(CanDragProperty, value); } | |
390 } | |
391 | |
392 /// <summary> | |
393 /// Gets the view mode | |
394 /// </summary> | |
395 public ViewModeType ViewMode | |
396 { | |
397 get { return (ViewModeType)base.GetValue(ViewModeProperty); } | |
398 set | |
399 { | |
400 base.SetValue(ViewModeProperty, value); | |
401 | |
402 if (value == ViewModeType.Busy) | |
403 { | |
404 this.GiveFocus(); | |
405 Application.Current.DoEvents(); | |
406 } | |
407 } | |
408 } | |
409 | |
410 #endregion | |
411 | |
412 #region · Constructors · | |
413 | |
414 /// <summary> | |
415 /// Initializes a new instance of the <see cref="WindowElement"/> class. | |
416 /// </summary> | |
417 public WindowElement() | |
418 : base() | |
419 { | |
420 } | |
421 | |
422 #endregion | |
423 | |
424 #region · Methods · | |
425 | |
426 public override void OnApplyTemplate() | |
427 { | |
428 base.OnApplyTemplate(); | |
429 | |
430 this.contentPresenter = this.GetTemplateChild(WindowElement.PART_ContentPresenter) as ContentPresenter; | |
431 } | |
432 | |
433 /// <summary> | |
434 /// Shows the window | |
435 /// </summary> | |
436 public void Show() | |
437 { | |
438 if (!this.isShowed) | |
439 { | |
440 this.isShowed = true; | |
441 } | |
442 | |
443 this.OnActivated(); | |
444 } | |
445 | |
446 /// <summary> | |
447 /// Shows the window as a modal dialog | |
448 /// </summary> | |
449 public DialogResult ShowDialog() | |
450 { | |
451 this.Parent = WindowElement.ModalContainerPanel; | |
452 this.isModal = true; | |
453 | |
454 this.LockKeyboardNavigation(); | |
455 this.LockMouseOutside(); | |
456 | |
457 this.Show(); | |
458 | |
459 // Set DialogResult default value | |
460 this.DialogResult = DialogResult.None; | |
461 | |
462 try | |
463 { | |
464 // Push the current thread to a modal state | |
465 ComponentDispatcher.PushModal(); | |
466 | |
467 // Create a DispatcherFrame instance and use it to start a message loop | |
468 this.dispatcherFrame = new DispatcherFrame(); | |
469 Dispatcher.PushFrame(this.dispatcherFrame); | |
470 } | |
471 finally | |
472 { | |
473 // Pop the current thread from modal state | |
474 ComponentDispatcher.PopModal(); | |
475 } | |
476 | |
477 return this.DialogResult; | |
478 } | |
479 | |
480 /// <summary> | |
481 /// Manually closes a <see cref="WindowElement"/>. | |
482 /// </summary> | |
483 public override void Close() | |
484 { | |
485 CancelEventArgs e = new CancelEventArgs(); | |
486 | |
487 if (this.Closing != null) | |
488 { | |
489 this.Closing(this, e); | |
490 } | |
491 | |
492 if (!e.Cancel) | |
493 { | |
494 this.LockMouseOutside(false); | |
495 | |
496 // Clean up | |
497 this.maximizeCommand = null; | |
498 this.minimizeCommand = null; | |
499 this.closeCommand = null; | |
500 this.dispatcherFrame = null; | |
501 this.isModal = false; | |
502 this.isShowed = false; | |
503 | |
504 this.CommandBindings.Clear(); | |
505 | |
506 base.Close(); | |
507 | |
508 if (this.Closed != null) | |
509 { | |
510 this.Closed(this, EventArgs.Empty); | |
511 } | |
512 } | |
513 } | |
514 | |
515 /// <summary> | |
516 /// Hides the Window | |
517 /// </summary> | |
518 public void Hide() | |
519 { | |
520 this.Visibility = System.Windows.Visibility.Collapsed; | |
521 | |
522 if (this.isModal && this.dispatcherFrame != null) | |
523 { | |
524 this.dispatcherFrame.Continue = false; | |
525 this.dispatcherFrame = null; | |
526 } | |
527 } | |
528 | |
529 #endregion | |
530 | |
531 #region · Protected Methods · | |
532 | |
533 /// <summary> | |
534 /// Focuses the window | |
535 /// </summary> | |
536 protected override void GiveFocus() | |
537 { | |
538 this.MoveFocus(FocusNavigationDirection.Next); | |
539 } | |
540 | |
541 /// <summary> | |
542 /// Invoked when an unhandled <see cref="E:System.Windows.Input.Keyboard.GotKeyboardFocus"/> attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event. | |
543 /// </summary> | |
544 /// <param name="e">The <see cref="T:System.Windows.Input.KeyboardFocusChangedEventArgs"/> that contains the event data.</param> | |
545 protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e) | |
546 { | |
547 base.OnGotKeyboardFocus(e); | |
548 this.OnActivated(); | |
549 } | |
550 | |
551 protected override void OnPreviewKeyDown(KeyEventArgs e) | |
552 { | |
553 if ((e.Key == Key.Enter || e.Key == Key.Return) && | |
554 Keyboard.Modifiers == ModifierKeys.None) | |
555 { | |
556 if (e.OriginalSource != this) | |
557 { | |
558 if (e.OriginalSource is DataGridCell || | |
559 e.OriginalSource is ComboBoxItem) | |
560 { | |
561 } | |
562 else | |
563 { | |
564 FocusNavigationDirection direction = FocusNavigationDirection.Next; | |
565 UIElement element = e.OriginalSource as UIElement; | |
566 | |
567 if (element != null) | |
568 { | |
569 element.MoveFocus(new TraversalRequest(direction)); | |
570 e.Handled = true; | |
571 } | |
572 } | |
573 } | |
574 } | |
575 | |
576 base.OnPreviewKeyDown(e); | |
577 } | |
578 | |
579 #endregion | |
580 | |
581 #region · Command Execution Methods · | |
582 | |
583 protected virtual void OnCloseWindow() | |
584 { | |
585 if (this.isModal) | |
586 { | |
587 this.Hide(); | |
588 } | |
589 else | |
590 { | |
591 this.Close(); | |
592 } | |
593 } | |
594 | |
595 protected virtual void OnMaximizeWindow() | |
596 { | |
597 if (this.WindowState != WindowState.Maximized) | |
598 { | |
599 this.WindowState = WindowState.Maximized; | |
600 } | |
601 else | |
602 { | |
603 this.WindowState = WindowState.Normal; | |
604 } | |
605 } | |
606 | |
607 protected virtual void OnMinimizeWindow() | |
608 { | |
609 if (this.WindowState != WindowState.Minimized) | |
610 { | |
611 this.WindowState = WindowState.Minimized; | |
612 } | |
613 else | |
614 { | |
615 this.WindowState = WindowState.Normal; | |
616 } | |
617 } | |
618 | |
619 #endregion | |
620 | |
621 #region · Private Methods · | |
622 | |
623 /// <summary> | |
624 /// http://blogs.msdn.com/b/ryantrem/archive/2010/04/09/globally-updating-binding-sources-in-wpf.aspx | |
625 /// </summary> | |
626 private void UpdateActiveElementBindings() | |
627 { | |
628 if (Keyboard.FocusedElement != null && | |
629 Keyboard.FocusedElement is DependencyObject) | |
630 { | |
631 DependencyObject element = (DependencyObject)Keyboard.FocusedElement; | |
632 LocalValueEnumerator localValueEnumerator = element.GetLocalValueEnumerator(); | |
633 | |
634 while (localValueEnumerator.MoveNext()) | |
635 { | |
636 BindingExpressionBase bindingExpression = BindingOperations.GetBindingExpressionBase(element, localValueEnumerator.Current.Property); | |
637 | |
638 if (bindingExpression != null) | |
639 { | |
640 bindingExpression.UpdateSource(); | |
641 bindingExpression.UpdateTarget(); | |
642 } | |
643 } | |
644 } | |
645 } | |
646 | |
647 private void LockKeyboardNavigation() | |
648 { | |
649 KeyboardNavigation.SetTabNavigation(this, KeyboardNavigationMode.Cycle); | |
650 KeyboardNavigation.SetDirectionalNavigation(this, KeyboardNavigationMode.None); | |
651 KeyboardNavigation.SetControlTabNavigation(this, KeyboardNavigationMode.None); | |
652 } | |
653 | |
654 private void LockMouseOutside() | |
655 { | |
656 this.LockMouseOutside(true); | |
657 } | |
658 | |
659 private void LockMouseOutside(bool doLock) | |
660 { | |
661 if (doLock) | |
662 { | |
663 Debug.Assert(WindowElement.ModalContainerPanel != null); | |
664 | |
665 Binding wBinding = new Binding("ActualWidth"); | |
666 Binding hBinding = new Binding("ActualHeight"); | |
667 Canvas fence = new Canvas(); | |
668 | |
669 wBinding.Source = WindowElement.ModalContainerPanel; | |
670 hBinding.Source = WindowElement.ModalContainerPanel; | |
671 | |
672 fence.SetBinding(Canvas.WidthProperty, wBinding); | |
673 fence.SetBinding(Canvas.HeightProperty, hBinding); | |
674 | |
675 fence.Background = new SolidColorBrush(Color.FromArgb(141, 162, 174, 255)); | |
676 fence.Opacity = 0.5; | |
677 | |
678 WindowElement.ModalContainerPanel.Children.Add(this); | |
679 WindowElement.ModalContainerPanel.Children.Add(fence); | |
680 WindowElement.ModalContainerPanel.BringToBottom(fence); | |
681 WindowElement.ModalContainerPanel.Visibility = System.Windows.Visibility.Visible; | |
682 } | |
683 else | |
684 { | |
685 Debug.Assert(WindowElement.ModalContainerPanel != null); | |
686 | |
687 for (int i = 0; i < WindowElement.ModalContainerPanel.Children.Count; i++) | |
688 { | |
689 UIElement target = WindowElement.ModalContainerPanel.Children[i]; | |
690 | |
691 if (target is Panel) | |
692 { | |
693 WindowElement.ModalContainerPanel.Children.Remove(target); | |
694 break; | |
695 } | |
696 } | |
697 | |
698 WindowElement.ModalContainerPanel.Children.Remove(this); | |
699 WindowElement.ModalContainerPanel.Visibility = System.Windows.Visibility.Hidden; | |
700 } | |
701 } | |
702 | |
703 private void UpdateWindowState(WindowState oldWindowState, WindowState newWindowState) | |
704 { | |
705 if (newWindowState == WindowState.Maximized) | |
706 { | |
707 if (oldWindowState == System.Windows.WindowState.Minimized) | |
708 { | |
709 this.Visibility = System.Windows.Visibility.Visible; | |
710 } | |
711 | |
712 VisualStateManager.GoToState(this, MaximizedVisualState, true); | |
713 | |
714 ScaleTransform st = this.contentPresenter.LayoutTransform as ScaleTransform; | |
715 | |
716 if (st != null) | |
717 { | |
718 this.OldPosition = this.GetPosition(); | |
719 | |
720 if (this.ConstraintToParent) | |
721 { | |
722 if (this.ActualWidth > this.ActualHeight) | |
723 { | |
724 st.ScaleX = Math.Round((this.Parent.ActualWidth - MaximizeMargin) / this.OriginalSize.Width, 2); | |
725 } | |
726 else | |
727 { | |
728 st.ScaleY = Math.Round((this.Parent.ActualHeight - MaximizeMargin) / this.OriginalSize.Height, 2); | |
729 } | |
730 | |
731 Application.Current.DoEvents(); | |
732 | |
733 this.MoveElement | |
734 ( | |
735 (this.Parent.ActualWidth - this.ActualWidth) / 2, | |
736 (this.Parent.ActualHeight - this.ActualHeight) / 2 | |
737 ); | |
738 } | |
739 else | |
740 { | |
741 double scaleX = Math.Round(this.GetParent<Window>().ActualWidth / this.OriginalSize.Width, 2); | |
742 double scaleY = Math.Round(this.GetParent<Window>().ActualHeight / this.OriginalSize.Height, 2); | |
743 | |
744 if (scaleX < scaleY) | |
745 { | |
746 st.ScaleX = (scaleX > 2.5) ? 2.5 : scaleX; | |
747 } | |
748 else | |
749 { | |
750 st.ScaleY = (scaleY > 2.5) ? 2.5 : scaleY; | |
751 } | |
752 | |
753 Application.Current.DoEvents(); | |
754 | |
755 this.MoveElement | |
756 ( | |
757 (this.GetParent<Window>().ActualWidth- this.ActualWidth) / 2, | |
758 (this.GetParent<Window>().ActualHeight - this.ActualHeight) / 2 - (this.GetParent<Window>().ActualHeight - this.Parent.ActualHeight) | |
759 ); | |
760 } | |
761 } | |
762 | |
763 this.Parent.BringToFront(this); | |
764 } | |
765 else if (newWindowState == WindowState.Normal) | |
766 { | |
767 this.Visibility = System.Windows.Visibility.Visible; | |
768 | |
769 VisualStateManager.GoToState(this, NormalVisualState, true); | |
770 | |
771 if (oldWindowState == WindowState.Minimized) | |
772 { | |
773 this.Activate(); | |
774 } | |
775 else if (oldWindowState == WindowState.Maximized) | |
776 { | |
777 ScaleTransform st = this.contentPresenter.LayoutTransform as ScaleTransform; | |
778 | |
779 if (st != null) | |
780 { | |
781 st.ScaleX = 1.0; | |
782 } | |
783 } | |
784 | |
785 this.MoveElement(this.OldPosition.X, this.OldPosition.Y); | |
786 } | |
787 else if (newWindowState == WindowState.Minimized) | |
788 { | |
789 VisualStateManager.GoToState(this, MinimizedVisualState, true); | |
790 | |
791 this.Visibility = Visibility.Collapsed; | |
792 this.oldWindowState = oldWindowState; | |
793 this.OldPosition = this.GetPosition(); | |
794 | |
795 this.OnDeactivated(); | |
796 } | |
797 } | |
798 | |
799 #endregion | |
800 } | |
801 } |