From c3e7ea28a892bcee3a467c90d260b767f31e9fb7 Mon Sep 17 00:00:00 2001 From: GF-Huang Date: Mon, 10 May 2021 22:23:31 +0800 Subject: [PATCH 1/3] Add KeepCenterOnSizeChanged attached property. --- .../Controls/Attach/WindowAttach.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs b/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs index f6b2e1938..4064f175f 100644 --- a/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs +++ b/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs @@ -145,5 +145,38 @@ public static void SetHideWhenClosing(DependencyObject element, bool value) public static bool GetHideWhenClosing(DependencyObject element) => (bool) element.GetValue(HideWhenClosingProperty); + + public static readonly DependencyProperty KeepCenterOnSizeChangedProperty = + DependencyProperty.RegisterAttached("KeepCenterOnSizeChanged", typeof(bool), typeof(WindowAttach), + new PropertyMetadata(ValueBoxes.FalseBox, KeepCenterOnSizeChangedPropertyChanged)); + + [AttachedPropertyBrowsableForType(typeof(System.Windows.Window))] + [AttachedPropertyBrowsableForType(typeof(Window))] + public static bool GetKeepCenterOnSizeChanged(DependencyObject obj) => (bool) obj.GetValue(KeepCenterOnSizeChangedProperty); + public static void SetKeepCenterOnSizeChanged(DependencyObject obj, bool value) => obj.SetValue(KeepCenterOnSizeChangedProperty, value); + + private static void KeepCenterOnSizeChangedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is Window window) + { + if ((bool) e.NewValue) + window.SizeChanged += Window_SizeChanged; + else + window.SizeChanged -= Window_SizeChanged; + } + + static void Window_SizeChanged(object sender, SizeChangedEventArgs e) + { + var window = (Window) sender; + if (window.WindowStartupLocation == WindowStartupLocation.CenterOwner || + window.WindowStartupLocation == WindowStartupLocation.CenterScreen) + { + if (e.WidthChanged) + window.Left += (e.PreviousSize.Width - e.NewSize.Width) / 2; + if (e.HeightChanged) + window.Top += (e.PreviousSize.Height - e.NewSize.Height) / 2; + } + } + } } } From 6c05c454fd59c6a6fab31d8c0c28dfd23d59f00d Mon Sep 17 00:00:00 2001 From: GF-Huang Date: Fri, 21 May 2021 09:49:00 +0800 Subject: [PATCH 2/3] Fix issue which typing in multi-line TextBox. --- .../HandyControl_Shared/Controls/Attach/WindowAttach.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs b/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs index 4064f175f..f933ea088 100644 --- a/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs +++ b/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs @@ -171,10 +171,15 @@ static void Window_SizeChanged(object sender, SizeChangedEventArgs e) if (window.WindowStartupLocation == WindowStartupLocation.CenterOwner || window.WindowStartupLocation == WindowStartupLocation.CenterScreen) { + var focusedElement = Keyboard.FocusedElement; + if (e.WidthChanged) window.Left += (e.PreviousSize.Width - e.NewSize.Width) / 2; if (e.HeightChanged) window.Top += (e.PreviousSize.Height - e.NewSize.Height) / 2; + + if (focusedElement != null) + Keyboard.Focus(focusedElement); } } } From 04ac854d72fefda99a27cd93bfd84e8242a0ae1b Mon Sep 17 00:00:00 2001 From: GF-Huang Date: Mon, 31 May 2021 15:54:27 +0800 Subject: [PATCH 3/3] Consider user manually resizing. --- .../HandyControl_Shared/Controls/Attach/WindowAttach.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs b/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs index f933ea088..fba717f96 100644 --- a/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs +++ b/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs @@ -168,8 +168,8 @@ private static void KeepCenterOnSizeChangedPropertyChanged(DependencyObject d, D static void Window_SizeChanged(object sender, SizeChangedEventArgs e) { var window = (Window) sender; - if (window.WindowStartupLocation == WindowStartupLocation.CenterOwner || - window.WindowStartupLocation == WindowStartupLocation.CenterScreen) + if (window.WindowStartupLocation != WindowStartupLocation.Manual && + window.SizeToContent != SizeToContent.Manual) { var focusedElement = Keyboard.FocusedElement;