diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index 74b12cc2be..1159fc4928 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -358,7 +358,7 @@ namespace Avalonia.Controls bool close = true; try - { + { if (!ignoreCancel && HandleClosing()) { close = false; @@ -369,16 +369,27 @@ namespace Avalonia.Controls { if (close) { - if (Owner is Window owner) - { - owner.RemoveChild(this); - } + CloseInternal(); + } + } + } - Owner = null; + private void CloseInternal () + { + foreach(var child in _children) + { + // if we HandleClosing() before then there will be no children. + child.CloseInternal(); + } - PlatformImpl?.Dispose(); - } + if (Owner is Window owner) + { + owner.RemoveChild(this); } + + Owner = null; + + PlatformImpl?.Dispose(); } /// @@ -386,10 +397,31 @@ namespace Avalonia.Controls /// protected virtual bool HandleClosing() { - var args = new CancelEventArgs(); - OnClosing(args); + bool canClose = true; - return args.Cancel; + foreach(var child in _children) + { + if(!child.HandleClosing()) + { + child.CloseInternal(); + } + else + { + canClose = false; + } + } + + if (canClose) + { + var args = new CancelEventArgs(); + OnClosing(args); + + return args.Cancel; + } + else + { + return !canClose; + } } protected virtual void HandleWindowStateChanged(WindowState state)