enhance: close #1205

This commit is contained in:
NaBian committed 2022-10-06 20:44:39 +08:00
1 parent 8b52df3a06
commit c544b0796e
2 files changed
+16 -5

No files matched your search

@@ -119,6 +119,15 @@ public class TabControl : System.Windows.Controls.TabControl
set => SetValue(ShowContextMenuProperty, ValueBoxes.BooleanBox(value)); set => SetValue(ShowContextMenuProperty, ValueBoxes.BooleanBox(value));
} }
public static readonly DependencyProperty CanBeClosedByMiddleButtonProperty =
DependencyProperty.Register("CanBeClosedByMiddleButton", typeof(bool), typeof(TabControl), new PropertyMetadata(ValueBoxes.TrueBox));
public bool CanBeClosedByMiddleButton
{
get => (bool) GetValue(CanBeClosedByMiddleButtonProperty);
set => SetValue(CanBeClosedByMiddleButtonProperty, ValueBoxes.BooleanBox(value));
}
/// <summary> /// <summary>
/// 是否将标签填充 /// 是否将标签填充
/// </summary> /// </summary>
@@ -242,10 +242,11 @@ public class TabItem : System.Windows.Controls.TabItem
public TabItem() public TabItem()
{ {
CommandBindings.Add(new CommandBinding(ControlCommands.Close, (s, e) => Close())); CommandBindings.Add(new CommandBinding(ControlCommands.Close, (s, e) => Close()));
CommandBindings.Add(new CommandBinding(ControlCommands.CloseAll, CommandBindings.Add(new CommandBinding(ControlCommands.CloseAll, (s, e) => TabControlParent.CloseAllItems()));
(s, e) => { TabControlParent.CloseAllItems(); })); CommandBindings.Add(new CommandBinding(
CommandBindings.Add(new CommandBinding(ControlCommands.CloseOther, ControlCommands.CloseOther,
(s, e) => { TabControlParent.CloseOtherItems(this); })); (s, e) => TabControlParent.CloseOtherItems(this),
(s, e) => e.CanExecute = TabControlParent.Items.Count > 1));
Loaded += (s, e) => OnMenuChanged(Menu); Loaded += (s, e) => OnMenuChanged(Menu);
} }
@@ -389,7 +390,8 @@ public class TabItem : System.Windows.Controls.TabItem
protected override void OnMouseDown(MouseButtonEventArgs e) protected override void OnMouseDown(MouseButtonEventArgs e)
{ {
if (e.ChangedButton == MouseButton.Middle && e.ButtonState == MouseButtonState.Pressed) if (e is { ChangedButton: MouseButton.Middle, ButtonState: MouseButtonState.Pressed } &&
TabControlParent.CanBeClosedByMiddleButton)
{ {
if (ShowCloseButton || ShowContextMenu) if (ShowCloseButton || ShowContextMenu)
{ {