Browse Source

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.
pull/1645/head
brianlagunas_cp 16 years ago
parent
commit
4873017096
  1. 97
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/ChildWindow/ChildWindow.cs

97
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;
/// <summary>
/// Gets or sets a value indicating whether the ChildWindow was accepted or canceled.
/// </summary>
/// <value>
/// True if the child window was accepted; false if the child window was
/// canceled. The default is null.
/// </value>
[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
/// <summary>
/// Occurs when the ChildWindow is closed.
/// </summary>
public event EventHandler Closed;
protected virtual void OnClosed(EventArgs e)
{
if (Closed != null)
Closed(this, e);
}
/// <summary>
/// Occurs when the ChildWindow is closing.
/// </summary>
public event EventHandler<CancelEventArgs> Closing;
protected virtual void OnClosing(CancelEventArgs e)
{
if (Closing != null)
Closing(this, e);
}
#endregion //Events
}
}

Loading…
Cancel
Save