diff --git a/src/Wpf.Ui/Controls/ClientAreaBorder.cs b/src/Wpf.Ui/Controls/ClientAreaBorder.cs index d15431069..ec8c53bc6 100644 --- a/src/Wpf.Ui/Controls/ClientAreaBorder.cs +++ b/src/Wpf.Ui/Controls/ClientAreaBorder.cs @@ -6,12 +6,14 @@ #nullable enable using System; +using System.ComponentModel; using System.Windows; using System.Windows.Media; using System.Windows.Shell; using Wpf.Ui.Appearance; using Wpf.Ui.Controls.Interfaces; using Wpf.Ui.Dpi; +using Wpf.Ui.Interop; using Size = System.Windows.Size; namespace Wpf.Ui.Controls; @@ -78,7 +80,7 @@ public Thickness PaddedBorderThickness /// /// If you use a to extend the client area of a window to the non-client area, you need to handle the edge margin issue when the window is maximized. /// Use this property to get the correct margin value when the window is maximized, so that when the window is maximized, the client area can completely cover the screen client area by no less than a single pixel at any DPI. - /// The method cannot obtain this value directly. + /// The method cannot obtain this value directly. /// public Thickness WindowChromeNonClientFrameThickness => _windowChromeNonClientFrameThickness ??= new Thickness( ResizeFrameBorderThickness.Left + PaddedBorderThickness.Left, @@ -110,6 +112,7 @@ protected override void OnVisualParentChanged(DependencyObject oldParent) if (_oldWindow is { } oldWindow) { oldWindow.StateChanged -= OnWindowStateChanged; + oldWindow.Closing -= OnWindowClosing; } var newWindow = (Window?)Window.GetWindow(this); @@ -118,13 +121,19 @@ protected override void OnVisualParentChanged(DependencyObject oldParent) { newWindow.StateChanged -= OnWindowStateChanged; // Unsafe newWindow.StateChanged += OnWindowStateChanged; + newWindow.Closing += OnWindowClosing; } _oldWindow = newWindow; ApplyDefaultWindowBorder(); } - + private void OnWindowClosing(object? sender, CancelEventArgs e) + { + Appearance.Theme.Changed -= OnThemeChanged; + if (_oldWindow != null) + _oldWindow.Closing -= OnWindowClosing; + } private void OnWindowStateChanged(object? sender, EventArgs e) { if (sender is not Window window) diff --git a/src/Wpf.Ui/Controls/TitleBar.cs b/src/Wpf.Ui/Controls/TitleBar.cs index ca9ad8e60..de058a413 100644 --- a/src/Wpf.Ui/Controls/TitleBar.cs +++ b/src/Wpf.Ui/Controls/TitleBar.cs @@ -450,7 +450,7 @@ private void CloseWindow() return; } - + Appearance.Theme.Changed -= OnThemeChanged; ParentWindow.Close(); }