From 4873017096823b1e3a9ea60eac794d3c72b443a1 Mon Sep 17 00:00:00 2001 From: brianlagunas_cp Date: Wed, 11 Aug 2010 01:33:10 +0000 Subject: [PATCH] ChildWindow: fixed WindowState bug ChildWindow: added DialogResult property, user can set the DialogResult to true/false/null and the window will close, when close button pressed dialog result is false. ChildWindow: can now show ChildWindow by using data binding on the WindowState property or by calling Show/Close in code. ChildWindow: added Closed and Closing events. During the Closing event a user can set e.Cancel to true and the close will be cancelled and Closed event will not fire. --- .../ChildWindow/ChildWindow.cs | 97 +++++++++++++++++-- 1 file changed, 90 insertions(+), 7 deletions(-) diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/ChildWindow/ChildWindow.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/ChildWindow/ChildWindow.cs index c22921cb..a92cf075 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/ChildWindow/ChildWindow.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/ChildWindow/ChildWindow.cs @@ -4,6 +4,7 @@ using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Controls.Primitives; +using System.ComponentModel; namespace Microsoft.Windows.Controls { @@ -224,7 +225,7 @@ namespace Microsoft.Windows.Controls public WindowState WindowState { get { return (WindowState)GetValue(WindowStateProperty); } - set { SetValue(WindowStateProperty, WindowState); } + set { SetValue(WindowStateProperty, value); } } private static void OnWindowStatePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) @@ -237,6 +238,28 @@ namespace Microsoft.Windows.Controls #endregion //Dependency Properties + private bool? _dialogResult; + /// + /// Gets or sets a value indicating whether the ChildWindow was accepted or canceled. + /// + /// + /// True if the child window was accepted; false if the child window was + /// canceled. The default is null. + /// + [TypeConverter(typeof(NullableBoolConverter))] + public bool? DialogResult + { + get { return _dialogResult; } + set + { + if (_dialogResult != value) + { + _dialogResult = value; + Close(); + } + } + } + #endregion //Properties #region Event Handlers @@ -327,18 +350,55 @@ namespace Microsoft.Windows.Controls { case WindowState.Closed: { - Visibility = System.Windows.Visibility.Hidden; + ExecuteClose(); break; } case WindowState.Open: { - Visibility = System.Windows.Visibility.Visible; - SetZIndex(); + ExecuteOpen(); break; } } } + private void ExecuteClose() + { + CancelEventArgs e = new CancelEventArgs(); + OnClosing(e); + + if (!e.Cancel) + { + Visibility = System.Windows.Visibility.Hidden; + + if (!_dialogResult.HasValue) + _dialogResult = false; + + OnClosed(EventArgs.Empty); + } + else + { + _dialogResult = null; //if the Close is cancelled, DialogResult should always be NULL: + } + } + + private void ExecuteOpen() + { + _dialogResult = null; //reset the dialogResult to null each time the window is opened + + Visibility = System.Windows.Visibility.Visible; + + if (_parent != null) + { + int parentIndex = (int)_parent.GetValue(Canvas.ZIndexProperty); + this.SetValue(Canvas.ZIndexProperty, ++parentIndex); + } + else + { + this.SetValue(Canvas.ZIndexProperty, 1); + } + } + + private void SetZIndex() { if (_parent != null) @@ -384,17 +444,40 @@ namespace Microsoft.Windows.Controls public void Show() { - SetWindowState(WindowState.Open); + WindowState = WindowState.Open; } - public void Close() { - SetWindowState(WindowState.Closed); + WindowState = WindowState.Closed; } #endregion //Public #endregion //Methods + + #region Events + + /// + /// Occurs when the ChildWindow is closed. + /// + public event EventHandler Closed; + protected virtual void OnClosed(EventArgs e) + { + if (Closed != null) + Closed(this, e); + } + + /// + /// Occurs when the ChildWindow is closing. + /// + public event EventHandler Closing; + protected virtual void OnClosing(CancelEventArgs e) + { + if (Closing != null) + Closing(this, e); + } + + #endregion //Events } }