From ef5ab72301d136bb39a53443b5c00dc58cd65013 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Sun, 24 Sep 2023 23:39:56 +0800 Subject: [PATCH] fix: close #1457 --- .../Controls/TabControl/TabItem.cs | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Shared/HandyControl_Shared/Controls/TabControl/TabItem.cs b/src/Shared/HandyControl_Shared/Controls/TabControl/TabItem.cs index d202527c..1b495414 100644 --- a/src/Shared/HandyControl_Shared/Controls/TabControl/TabItem.cs +++ b/src/Shared/HandyControl_Shared/Controls/TabControl/TabItem.cs @@ -257,7 +257,11 @@ protected override void OnMouseRightButtonDown(MouseButtonEventArgs e) { base.OnMouseRightButtonDown(e); - if (VisualTreeHelper.HitTest(this, e.GetPosition(this)) == null) return; + if (!IsMouseOverHeader(e)) + { + return; + } + IsSelected = true; Focus(); } @@ -300,7 +304,11 @@ protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); - if (VisualTreeHelper.HitTest(this, e.GetPosition(this)) == null) return; + if (!IsMouseOverHeader(e)) + { + return; + } + var parent = TabControlParent; if (parent == null) return; @@ -391,7 +399,8 @@ protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) protected override void OnMouseDown(MouseButtonEventArgs e) { if (e is { ChangedButton: MouseButton.Middle, ButtonState: MouseButtonState.Pressed } && - TabControlParent.CanBeClosedByMiddleButton) + TabControlParent.CanBeClosedByMiddleButton && + IsMouseOverHeader(e)) { if (ShowCloseButton || ShowContextMenu) { @@ -474,6 +483,11 @@ private int CalLocationIndex(double left) return result > maxIndex ? maxIndex : result; } + private bool IsMouseOverHeader(MouseButtonEventArgs e) + { + return VisualTreeHelper.HitTest(this, e.GetPosition(this)) is not null; + } + public static readonly RoutedEvent ClosingEvent = EventManager.RegisterRoutedEvent("Closing", RoutingStrategy.Bubble, typeof(EventHandler), typeof(TabItem)); public event EventHandler Closing