diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index 988a265655..0b33ed6431 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -62,6 +62,25 @@ namespace Avalonia.Controls Full = 2 } + /// + /// Describes how the event behaves in the presence of child windows. + /// + public enum WindowClosingBehavior + { + /// + /// When the owner window is closed, the child windows' event + /// will be raised, followed by the owner window's events. A child + /// canceling the close will result in the owner Window's close being cancelled. + /// + OwnerAndChildWindows, + + /// + /// When the owner window is closed, only the owner window's event + /// will be raised. This behavior is the same as WPF's. + /// + OwnerWindowOnly, + } + /// /// A top-level window. /// @@ -127,6 +146,12 @@ namespace Avalonia.Controls public static readonly StyledProperty ShowInTaskbarProperty = AvaloniaProperty.Register(nameof(ShowInTaskbar), true); + /// + /// Defines the property. + /// + public static readonly StyledProperty ClosingBehaviorProperty = + AvaloniaProperty.Register(nameof(ClosingBehavior)); + /// /// Represents the current window state (normal, minimized, maximized) /// @@ -347,6 +372,16 @@ namespace Avalonia.Controls set => SetValue(ShowInTaskbarProperty, value); } + /// + /// Gets or sets a value indicating how the event behaves in the presence + /// of child windows. + /// + public WindowClosingBehavior ClosingBehavior + { + get => GetValue(ClosingBehaviorProperty); + set => SetValue(ClosingBehaviorProperty, value); + } + /// /// Gets or sets the minimized/maximized state of the window. /// @@ -487,31 +522,41 @@ namespace Avalonia.Controls private bool ShouldCancelClose(WindowClosingEventArgs args) { - bool canClose = true; - - if (_children.Count > 0) + switch (ClosingBehavior) { - var childArgs = args.CloseReason == WindowCloseReason.WindowClosing ? - new WindowClosingEventArgs(WindowCloseReason.OwnerWindowClosing, args.IsProgrammatic) : - args; + case WindowClosingBehavior.OwnerAndChildWindows: + bool canClose = true; - foreach (var (child, _) in _children.ToArray()) - { - if (child.ShouldCancelClose(childArgs)) + if (_children.Count > 0) { - canClose = false; + var childArgs = args.CloseReason == WindowCloseReason.WindowClosing ? + new WindowClosingEventArgs(WindowCloseReason.OwnerWindowClosing, args.IsProgrammatic) : + args; + + foreach (var (child, _) in _children.ToArray()) + { + if (child.ShouldCancelClose(childArgs)) + { + canClose = false; + } + } } - } - } - if (canClose) - { - OnClosing(args); + if (canClose) + { + OnClosing(args); - return args.Cancel; + return args.Cancel; + } + + return true; + case WindowClosingBehavior.OwnerWindowOnly: + OnClosing(args); + + return args.Cancel; } - return true; + return false; } private void HandleWindowStateChanged(WindowState state)