chore: use file-scoped namespace
This commit is contained in:
1 parent
99d2fafb72
commit
bbf1b7d4c6
748 files changed
+57929
-58678
No files matched your search
@@ -8,422 +8,421 @@ using System.Windows.Input;
|
||||
using HandyControl.Data;
|
||||
using HandyControl.Tools.Extension;
|
||||
|
||||
namespace HandyControl.Controls
|
||||
namespace HandyControl.Controls;
|
||||
|
||||
[TemplatePart(Name = OverflowButtonKey, Type = typeof(ContextMenuToggleButton))]
|
||||
[TemplatePart(Name = HeaderPanelKey, Type = typeof(TabPanel))]
|
||||
[TemplatePart(Name = OverflowScrollviewer, Type = typeof(ScrollViewer))]
|
||||
[TemplatePart(Name = ScrollButtonLeft, Type = typeof(ButtonBase))]
|
||||
[TemplatePart(Name = ScrollButtonRight, Type = typeof(ButtonBase))]
|
||||
[TemplatePart(Name = HeaderBorder, Type = typeof(Border))]
|
||||
public class TabControl : System.Windows.Controls.TabControl
|
||||
{
|
||||
[TemplatePart(Name = OverflowButtonKey, Type = typeof(ContextMenuToggleButton))]
|
||||
[TemplatePart(Name = HeaderPanelKey, Type = typeof(TabPanel))]
|
||||
[TemplatePart(Name = OverflowScrollviewer, Type = typeof(ScrollViewer))]
|
||||
[TemplatePart(Name = ScrollButtonLeft, Type = typeof(ButtonBase))]
|
||||
[TemplatePart(Name = ScrollButtonRight, Type = typeof(ButtonBase))]
|
||||
[TemplatePart(Name = HeaderBorder, Type = typeof(Border))]
|
||||
public class TabControl : System.Windows.Controls.TabControl
|
||||
private const string OverflowButtonKey = "PART_OverflowButton";
|
||||
|
||||
private const string HeaderPanelKey = "PART_HeaderPanel";
|
||||
|
||||
private const string OverflowScrollviewer = "PART_OverflowScrollviewer";
|
||||
|
||||
private const string ScrollButtonLeft = "PART_ScrollButtonLeft";
|
||||
|
||||
private const string ScrollButtonRight = "PART_ScrollButtonRight";
|
||||
|
||||
private const string HeaderBorder = "PART_HeaderBorder";
|
||||
|
||||
private ContextMenuToggleButton _buttonOverflow;
|
||||
|
||||
internal TabPanel HeaderPanel { get; private set; }
|
||||
|
||||
private ScrollViewer _scrollViewerOverflow;
|
||||
|
||||
private ButtonBase _buttonScrollLeft;
|
||||
|
||||
private ButtonBase _buttonScrollRight;
|
||||
|
||||
private Border _headerBorder;
|
||||
|
||||
/// <summary>
|
||||
/// 是否为内部操作
|
||||
/// </summary>
|
||||
internal bool IsInternalAction;
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用动画
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsAnimationEnabledProperty = DependencyProperty.Register(
|
||||
"IsAnimationEnabled", typeof(bool), typeof(TabControl), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用动画
|
||||
/// </summary>
|
||||
public bool IsAnimationEnabled
|
||||
{
|
||||
private const string OverflowButtonKey = "PART_OverflowButton";
|
||||
get => (bool) GetValue(IsAnimationEnabledProperty);
|
||||
set => SetValue(IsAnimationEnabledProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
private const string HeaderPanelKey = "PART_HeaderPanel";
|
||||
/// <summary>
|
||||
/// 是否可以拖动
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsDraggableProperty = DependencyProperty.Register(
|
||||
"IsDraggable", typeof(bool), typeof(TabControl), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
private const string OverflowScrollviewer = "PART_OverflowScrollviewer";
|
||||
/// <summary>
|
||||
/// 是否可以拖动
|
||||
/// </summary>
|
||||
public bool IsDraggable
|
||||
{
|
||||
get => (bool) GetValue(IsDraggableProperty);
|
||||
set => SetValue(IsDraggableProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
private const string ScrollButtonLeft = "PART_ScrollButtonLeft";
|
||||
/// <summary>
|
||||
/// 是否显示关闭按钮
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShowCloseButtonProperty = DependencyProperty.RegisterAttached(
|
||||
"ShowCloseButton", typeof(bool), typeof(TabControl), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits));
|
||||
|
||||
private const string ScrollButtonRight = "PART_ScrollButtonRight";
|
||||
public static void SetShowCloseButton(DependencyObject element, bool value)
|
||||
=> element.SetValue(ShowCloseButtonProperty, ValueBoxes.BooleanBox(value));
|
||||
|
||||
private const string HeaderBorder = "PART_HeaderBorder";
|
||||
public static bool GetShowCloseButton(DependencyObject element)
|
||||
=> (bool) element.GetValue(ShowCloseButtonProperty);
|
||||
|
||||
private ContextMenuToggleButton _buttonOverflow;
|
||||
/// <summary>
|
||||
/// 是否显示关闭按钮
|
||||
/// </summary>
|
||||
public bool ShowCloseButton
|
||||
{
|
||||
get => (bool) GetValue(ShowCloseButtonProperty);
|
||||
set => SetValue(ShowCloseButtonProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
internal TabPanel HeaderPanel { get; private set; }
|
||||
/// <summary>
|
||||
/// 是否显示上下文菜单
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShowContextMenuProperty = DependencyProperty.RegisterAttached(
|
||||
"ShowContextMenu", typeof(bool), typeof(TabControl), new FrameworkPropertyMetadata(ValueBoxes.TrueBox, FrameworkPropertyMetadataOptions.Inherits));
|
||||
|
||||
private ScrollViewer _scrollViewerOverflow;
|
||||
public static void SetShowContextMenu(DependencyObject element, bool value)
|
||||
=> element.SetValue(ShowContextMenuProperty, ValueBoxes.BooleanBox(value));
|
||||
|
||||
private ButtonBase _buttonScrollLeft;
|
||||
public static bool GetShowContextMenu(DependencyObject element)
|
||||
=> (bool) element.GetValue(ShowContextMenuProperty);
|
||||
|
||||
private ButtonBase _buttonScrollRight;
|
||||
/// <summary>
|
||||
/// 是否显示上下文菜单
|
||||
/// </summary>
|
||||
public bool ShowContextMenu
|
||||
{
|
||||
get => (bool) GetValue(ShowContextMenuProperty);
|
||||
set => SetValue(ShowContextMenuProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
private Border _headerBorder;
|
||||
/// <summary>
|
||||
/// 是否将标签填充
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsTabFillEnabledProperty = DependencyProperty.Register(
|
||||
"IsTabFillEnabled", typeof(bool), typeof(TabControl), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
/// <summary>
|
||||
/// 是否为内部操作
|
||||
/// </summary>
|
||||
internal bool IsInternalAction;
|
||||
/// <summary>
|
||||
/// 是否将标签填充
|
||||
/// </summary>
|
||||
public bool IsTabFillEnabled
|
||||
{
|
||||
get => (bool) GetValue(IsTabFillEnabledProperty);
|
||||
set => SetValue(IsTabFillEnabledProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用动画
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsAnimationEnabledProperty = DependencyProperty.Register(
|
||||
"IsAnimationEnabled", typeof(bool), typeof(TabControl), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
/// <summary>
|
||||
/// 标签宽度
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty TabItemWidthProperty = DependencyProperty.Register(
|
||||
"TabItemWidth", typeof(double), typeof(TabControl), new PropertyMetadata(200.0));
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用动画
|
||||
/// </summary>
|
||||
public bool IsAnimationEnabled
|
||||
/// <summary>
|
||||
/// 标签宽度
|
||||
/// </summary>
|
||||
public double TabItemWidth
|
||||
{
|
||||
get => (double) GetValue(TabItemWidthProperty);
|
||||
set => SetValue(TabItemWidthProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标签高度
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty TabItemHeightProperty = DependencyProperty.Register(
|
||||
"TabItemHeight", typeof(double), typeof(TabControl), new PropertyMetadata(30.0));
|
||||
|
||||
/// <summary>
|
||||
/// 标签高度
|
||||
/// </summary>
|
||||
public double TabItemHeight
|
||||
{
|
||||
get => (double) GetValue(TabItemHeightProperty);
|
||||
set => SetValue(TabItemHeightProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以滚动
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsScrollableProperty = DependencyProperty.Register(
|
||||
"IsScrollable", typeof(bool), typeof(TabControl), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以滚动
|
||||
/// </summary>
|
||||
public bool IsScrollable
|
||||
{
|
||||
get => (bool) GetValue(IsScrollableProperty);
|
||||
set => SetValue(IsScrollableProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示溢出按钮
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShowOverflowButtonProperty = DependencyProperty.Register(
|
||||
"ShowOverflowButton", typeof(bool), typeof(TabControl), new PropertyMetadata(ValueBoxes.TrueBox));
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示溢出按钮
|
||||
/// </summary>
|
||||
public bool ShowOverflowButton
|
||||
{
|
||||
get => (bool) GetValue(ShowOverflowButtonProperty);
|
||||
set => SetValue(ShowOverflowButtonProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示滚动按钮
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShowScrollButtonProperty = DependencyProperty.Register(
|
||||
"ShowScrollButton", typeof(bool), typeof(TabControl), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示滚动按钮
|
||||
/// </summary>
|
||||
public bool ShowScrollButton
|
||||
{
|
||||
get => (bool) GetValue(ShowScrollButtonProperty);
|
||||
set => SetValue(ShowScrollButtonProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 可见的标签数量
|
||||
/// </summary>
|
||||
private int _itemShowCount;
|
||||
|
||||
protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
base.OnItemsChanged(e);
|
||||
|
||||
if (HeaderPanel == null)
|
||||
{
|
||||
get => (bool) GetValue(IsAnimationEnabledProperty);
|
||||
set => SetValue(IsAnimationEnabledProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以拖动
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsDraggableProperty = DependencyProperty.Register(
|
||||
"IsDraggable", typeof(bool), typeof(TabControl), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以拖动
|
||||
/// </summary>
|
||||
public bool IsDraggable
|
||||
{
|
||||
get => (bool) GetValue(IsDraggableProperty);
|
||||
set => SetValue(IsDraggableProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示关闭按钮
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShowCloseButtonProperty = DependencyProperty.RegisterAttached(
|
||||
"ShowCloseButton", typeof(bool), typeof(TabControl), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits));
|
||||
|
||||
public static void SetShowCloseButton(DependencyObject element, bool value)
|
||||
=> element.SetValue(ShowCloseButtonProperty, ValueBoxes.BooleanBox(value));
|
||||
|
||||
public static bool GetShowCloseButton(DependencyObject element)
|
||||
=> (bool) element.GetValue(ShowCloseButtonProperty);
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示关闭按钮
|
||||
/// </summary>
|
||||
public bool ShowCloseButton
|
||||
{
|
||||
get => (bool) GetValue(ShowCloseButtonProperty);
|
||||
set => SetValue(ShowCloseButtonProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示上下文菜单
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShowContextMenuProperty = DependencyProperty.RegisterAttached(
|
||||
"ShowContextMenu", typeof(bool), typeof(TabControl), new FrameworkPropertyMetadata(ValueBoxes.TrueBox, FrameworkPropertyMetadataOptions.Inherits));
|
||||
|
||||
public static void SetShowContextMenu(DependencyObject element, bool value)
|
||||
=> element.SetValue(ShowContextMenuProperty, ValueBoxes.BooleanBox(value));
|
||||
|
||||
public static bool GetShowContextMenu(DependencyObject element)
|
||||
=> (bool) element.GetValue(ShowContextMenuProperty);
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示上下文菜单
|
||||
/// </summary>
|
||||
public bool ShowContextMenu
|
||||
{
|
||||
get => (bool) GetValue(ShowContextMenuProperty);
|
||||
set => SetValue(ShowContextMenuProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否将标签填充
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsTabFillEnabledProperty = DependencyProperty.Register(
|
||||
"IsTabFillEnabled", typeof(bool), typeof(TabControl), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
/// <summary>
|
||||
/// 是否将标签填充
|
||||
/// </summary>
|
||||
public bool IsTabFillEnabled
|
||||
{
|
||||
get => (bool) GetValue(IsTabFillEnabledProperty);
|
||||
set => SetValue(IsTabFillEnabledProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标签宽度
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty TabItemWidthProperty = DependencyProperty.Register(
|
||||
"TabItemWidth", typeof(double), typeof(TabControl), new PropertyMetadata(200.0));
|
||||
|
||||
/// <summary>
|
||||
/// 标签宽度
|
||||
/// </summary>
|
||||
public double TabItemWidth
|
||||
{
|
||||
get => (double) GetValue(TabItemWidthProperty);
|
||||
set => SetValue(TabItemWidthProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标签高度
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty TabItemHeightProperty = DependencyProperty.Register(
|
||||
"TabItemHeight", typeof(double), typeof(TabControl), new PropertyMetadata(30.0));
|
||||
|
||||
/// <summary>
|
||||
/// 标签高度
|
||||
/// </summary>
|
||||
public double TabItemHeight
|
||||
{
|
||||
get => (double) GetValue(TabItemHeightProperty);
|
||||
set => SetValue(TabItemHeightProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以滚动
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsScrollableProperty = DependencyProperty.Register(
|
||||
"IsScrollable", typeof(bool), typeof(TabControl), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以滚动
|
||||
/// </summary>
|
||||
public bool IsScrollable
|
||||
{
|
||||
get => (bool) GetValue(IsScrollableProperty);
|
||||
set => SetValue(IsScrollableProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示溢出按钮
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShowOverflowButtonProperty = DependencyProperty.Register(
|
||||
"ShowOverflowButton", typeof(bool), typeof(TabControl), new PropertyMetadata(ValueBoxes.TrueBox));
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示溢出按钮
|
||||
/// </summary>
|
||||
public bool ShowOverflowButton
|
||||
{
|
||||
get => (bool) GetValue(ShowOverflowButtonProperty);
|
||||
set => SetValue(ShowOverflowButtonProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示滚动按钮
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShowScrollButtonProperty = DependencyProperty.Register(
|
||||
"ShowScrollButton", typeof(bool), typeof(TabControl), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示滚动按钮
|
||||
/// </summary>
|
||||
public bool ShowScrollButton
|
||||
{
|
||||
get => (bool) GetValue(ShowScrollButtonProperty);
|
||||
set => SetValue(ShowScrollButtonProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 可见的标签数量
|
||||
/// </summary>
|
||||
private int _itemShowCount;
|
||||
|
||||
protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
base.OnItemsChanged(e);
|
||||
|
||||
if (HeaderPanel == null)
|
||||
{
|
||||
IsInternalAction = false;
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateOverflowButton();
|
||||
|
||||
if (IsInternalAction)
|
||||
{
|
||||
IsInternalAction = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.Action == NotifyCollectionChangedAction.Add)
|
||||
{
|
||||
for (var i = 0; i < Items.Count; i++)
|
||||
{
|
||||
if (ItemContainerGenerator.ContainerFromIndex(i) is not TabItem item) return;
|
||||
item.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
|
||||
item.TabPanel = HeaderPanel;
|
||||
}
|
||||
}
|
||||
|
||||
_headerBorder?.InvalidateMeasure();
|
||||
IsInternalAction = false;
|
||||
return;
|
||||
}
|
||||
|
||||
public override void OnApplyTemplate()
|
||||
UpdateOverflowButton();
|
||||
|
||||
if (IsInternalAction)
|
||||
{
|
||||
if (_buttonOverflow != null)
|
||||
{
|
||||
if (_buttonOverflow.Menu != null)
|
||||
{
|
||||
_buttonOverflow.Menu.Closed -= Menu_Closed;
|
||||
_buttonOverflow.Menu = null;
|
||||
}
|
||||
|
||||
_buttonOverflow.Click -= ButtonOverflow_Click;
|
||||
}
|
||||
|
||||
if (_buttonScrollLeft != null) _buttonScrollLeft.Click -= ButtonScrollLeft_Click;
|
||||
if (_buttonScrollRight != null) _buttonScrollRight.Click -= ButtonScrollRight_Click;
|
||||
|
||||
base.OnApplyTemplate();
|
||||
HeaderPanel = GetTemplateChild(HeaderPanelKey) as TabPanel;
|
||||
|
||||
if (IsTabFillEnabled) return;
|
||||
|
||||
_buttonOverflow = GetTemplateChild(OverflowButtonKey) as ContextMenuToggleButton;
|
||||
_scrollViewerOverflow = GetTemplateChild(OverflowScrollviewer) as ScrollViewer;
|
||||
_buttonScrollLeft = GetTemplateChild(ScrollButtonLeft) as ButtonBase;
|
||||
_buttonScrollRight = GetTemplateChild(ScrollButtonRight) as ButtonBase;
|
||||
_headerBorder = GetTemplateChild(HeaderBorder) as Border;
|
||||
|
||||
if (_buttonScrollLeft != null) _buttonScrollLeft.Click += ButtonScrollLeft_Click;
|
||||
if (_buttonScrollRight != null) _buttonScrollRight.Click += ButtonScrollRight_Click;
|
||||
|
||||
if (_buttonOverflow != null)
|
||||
{
|
||||
var menu = new ContextMenu
|
||||
{
|
||||
Placement = PlacementMode.Bottom,
|
||||
PlacementTarget = _buttonOverflow
|
||||
};
|
||||
menu.Closed += Menu_Closed;
|
||||
_buttonOverflow.Menu = menu;
|
||||
_buttonOverflow.Click += ButtonOverflow_Click;
|
||||
}
|
||||
IsInternalAction = false;
|
||||
return;
|
||||
}
|
||||
|
||||
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
|
||||
if (e.Action == NotifyCollectionChangedAction.Add)
|
||||
{
|
||||
base.OnRenderSizeChanged(sizeInfo);
|
||||
UpdateOverflowButton();
|
||||
}
|
||||
|
||||
private void UpdateOverflowButton()
|
||||
{
|
||||
if (!IsTabFillEnabled)
|
||||
{
|
||||
_itemShowCount = (int) (ActualWidth / TabItemWidth);
|
||||
_buttonOverflow?.Show(ShowOverflowButton && Items.Count > 0 && Items.Count >= _itemShowCount);
|
||||
}
|
||||
}
|
||||
|
||||
private void Menu_Closed(object sender, RoutedEventArgs e) => _buttonOverflow.IsChecked = false;
|
||||
|
||||
private void ButtonScrollRight_Click(object sender, RoutedEventArgs e) =>
|
||||
_scrollViewerOverflow.ScrollToHorizontalOffsetWithAnimation(Math.Min(
|
||||
_scrollViewerOverflow.CurrentHorizontalOffset + TabItemWidth, _scrollViewerOverflow.ScrollableWidth));
|
||||
|
||||
private void ButtonScrollLeft_Click(object sender, RoutedEventArgs e) =>
|
||||
_scrollViewerOverflow.ScrollToHorizontalOffsetWithAnimation(Math.Max(
|
||||
_scrollViewerOverflow.CurrentHorizontalOffset - TabItemWidth, 0));
|
||||
|
||||
private void ButtonOverflow_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (_buttonOverflow.IsChecked == true)
|
||||
{
|
||||
_buttonOverflow.Menu.Items.Clear();
|
||||
for (var i = 0; i < Items.Count; i++)
|
||||
{
|
||||
if (ItemContainerGenerator.ContainerFromIndex(i) is not TabItem item) continue;
|
||||
|
||||
var menuItem = new MenuItem
|
||||
{
|
||||
HeaderStringFormat = ItemStringFormat,
|
||||
HeaderTemplate = ItemTemplate,
|
||||
HeaderTemplateSelector = ItemTemplateSelector,
|
||||
Header = item.Header,
|
||||
Width = TabItemWidth,
|
||||
IsChecked = item.IsSelected,
|
||||
IsCheckable = true,
|
||||
IsEnabled = item.IsEnabled
|
||||
};
|
||||
|
||||
menuItem.Click += delegate
|
||||
{
|
||||
_buttonOverflow.IsChecked = false;
|
||||
|
||||
var list = GetActualList();
|
||||
if (list == null) return;
|
||||
|
||||
var actualItem = ItemContainerGenerator.ItemFromContainer(item);
|
||||
if (actualItem == null) return;
|
||||
|
||||
var index = list.IndexOf(actualItem);
|
||||
if (index >= _itemShowCount)
|
||||
{
|
||||
list.Remove(actualItem);
|
||||
list.Insert(0, actualItem);
|
||||
HeaderPanel.SetValue(TabPanel.FluidMoveDurationPropertyKey,
|
||||
IsAnimationEnabled
|
||||
? new Duration(TimeSpan.FromMilliseconds(200))
|
||||
: new Duration(TimeSpan.FromMilliseconds(0)));
|
||||
HeaderPanel.ForceUpdate = true;
|
||||
HeaderPanel.Measure(new Size(HeaderPanel.DesiredSize.Width, ActualHeight));
|
||||
HeaderPanel.ForceUpdate = false;
|
||||
SetCurrentValue(SelectedIndexProperty, ValueBoxes.Int0Box);
|
||||
}
|
||||
|
||||
item.IsSelected = true;
|
||||
};
|
||||
_buttonOverflow.Menu.Items.Add(menuItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal double GetHorizontalOffset() => _scrollViewerOverflow?.CurrentHorizontalOffset ?? 0;
|
||||
|
||||
internal void UpdateScroll() => _scrollViewerOverflow?.RaiseEvent(new MouseWheelEventArgs(Mouse.PrimaryDevice, Environment.TickCount, 0)
|
||||
{
|
||||
RoutedEvent = MouseWheelEvent
|
||||
});
|
||||
|
||||
internal void CloseAllItems() => CloseOtherItems(null);
|
||||
|
||||
internal void CloseOtherItems(TabItem currentItem)
|
||||
{
|
||||
var actualItem = currentItem != null ? ItemContainerGenerator.ItemFromContainer(currentItem) : null;
|
||||
|
||||
var list = GetActualList();
|
||||
if (list == null) return;
|
||||
|
||||
IsInternalAction = true;
|
||||
|
||||
for (var i = 0; i < Items.Count; i++)
|
||||
{
|
||||
var item = list[i];
|
||||
if (!Equals(item, actualItem) && item != null)
|
||||
{
|
||||
var argsClosing = new CancelRoutedEventArgs(TabItem.ClosingEvent, item);
|
||||
|
||||
if (ItemContainerGenerator.ContainerFromItem(item) is not TabItem tabItem) continue;
|
||||
|
||||
tabItem.RaiseEvent(argsClosing);
|
||||
if (argsClosing.Cancel) return;
|
||||
|
||||
tabItem.RaiseEvent(new RoutedEventArgs(TabItem.ClosedEvent, item));
|
||||
list.Remove(item);
|
||||
|
||||
i--;
|
||||
}
|
||||
if (ItemContainerGenerator.ContainerFromIndex(i) is not TabItem item) return;
|
||||
item.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
|
||||
item.TabPanel = HeaderPanel;
|
||||
}
|
||||
|
||||
SetCurrentValue(SelectedIndexProperty, Items.Count == 0 ? -1 : 0);
|
||||
}
|
||||
|
||||
internal IList GetActualList()
|
||||
{
|
||||
IList list;
|
||||
if (ItemsSource != null)
|
||||
{
|
||||
list = ItemsSource as IList;
|
||||
}
|
||||
else
|
||||
{
|
||||
list = Items;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
protected override bool IsItemItsOwnContainerOverride(object item) => item is TabItem;
|
||||
|
||||
protected override DependencyObject GetContainerForItemOverride() => new TabItem();
|
||||
_headerBorder?.InvalidateMeasure();
|
||||
IsInternalAction = false;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnApplyTemplate()
|
||||
{
|
||||
if (_buttonOverflow != null)
|
||||
{
|
||||
if (_buttonOverflow.Menu != null)
|
||||
{
|
||||
_buttonOverflow.Menu.Closed -= Menu_Closed;
|
||||
_buttonOverflow.Menu = null;
|
||||
}
|
||||
|
||||
_buttonOverflow.Click -= ButtonOverflow_Click;
|
||||
}
|
||||
|
||||
if (_buttonScrollLeft != null) _buttonScrollLeft.Click -= ButtonScrollLeft_Click;
|
||||
if (_buttonScrollRight != null) _buttonScrollRight.Click -= ButtonScrollRight_Click;
|
||||
|
||||
base.OnApplyTemplate();
|
||||
HeaderPanel = GetTemplateChild(HeaderPanelKey) as TabPanel;
|
||||
|
||||
if (IsTabFillEnabled) return;
|
||||
|
||||
_buttonOverflow = GetTemplateChild(OverflowButtonKey) as ContextMenuToggleButton;
|
||||
_scrollViewerOverflow = GetTemplateChild(OverflowScrollviewer) as ScrollViewer;
|
||||
_buttonScrollLeft = GetTemplateChild(ScrollButtonLeft) as ButtonBase;
|
||||
_buttonScrollRight = GetTemplateChild(ScrollButtonRight) as ButtonBase;
|
||||
_headerBorder = GetTemplateChild(HeaderBorder) as Border;
|
||||
|
||||
if (_buttonScrollLeft != null) _buttonScrollLeft.Click += ButtonScrollLeft_Click;
|
||||
if (_buttonScrollRight != null) _buttonScrollRight.Click += ButtonScrollRight_Click;
|
||||
|
||||
if (_buttonOverflow != null)
|
||||
{
|
||||
var menu = new ContextMenu
|
||||
{
|
||||
Placement = PlacementMode.Bottom,
|
||||
PlacementTarget = _buttonOverflow
|
||||
};
|
||||
menu.Closed += Menu_Closed;
|
||||
_buttonOverflow.Menu = menu;
|
||||
_buttonOverflow.Click += ButtonOverflow_Click;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
|
||||
{
|
||||
base.OnRenderSizeChanged(sizeInfo);
|
||||
UpdateOverflowButton();
|
||||
}
|
||||
|
||||
private void UpdateOverflowButton()
|
||||
{
|
||||
if (!IsTabFillEnabled)
|
||||
{
|
||||
_itemShowCount = (int) (ActualWidth / TabItemWidth);
|
||||
_buttonOverflow?.Show(ShowOverflowButton && Items.Count > 0 && Items.Count >= _itemShowCount);
|
||||
}
|
||||
}
|
||||
|
||||
private void Menu_Closed(object sender, RoutedEventArgs e) => _buttonOverflow.IsChecked = false;
|
||||
|
||||
private void ButtonScrollRight_Click(object sender, RoutedEventArgs e) =>
|
||||
_scrollViewerOverflow.ScrollToHorizontalOffsetWithAnimation(Math.Min(
|
||||
_scrollViewerOverflow.CurrentHorizontalOffset + TabItemWidth, _scrollViewerOverflow.ScrollableWidth));
|
||||
|
||||
private void ButtonScrollLeft_Click(object sender, RoutedEventArgs e) =>
|
||||
_scrollViewerOverflow.ScrollToHorizontalOffsetWithAnimation(Math.Max(
|
||||
_scrollViewerOverflow.CurrentHorizontalOffset - TabItemWidth, 0));
|
||||
|
||||
private void ButtonOverflow_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (_buttonOverflow.IsChecked == true)
|
||||
{
|
||||
_buttonOverflow.Menu.Items.Clear();
|
||||
for (var i = 0; i < Items.Count; i++)
|
||||
{
|
||||
if (ItemContainerGenerator.ContainerFromIndex(i) is not TabItem item) continue;
|
||||
|
||||
var menuItem = new MenuItem
|
||||
{
|
||||
HeaderStringFormat = ItemStringFormat,
|
||||
HeaderTemplate = ItemTemplate,
|
||||
HeaderTemplateSelector = ItemTemplateSelector,
|
||||
Header = item.Header,
|
||||
Width = TabItemWidth,
|
||||
IsChecked = item.IsSelected,
|
||||
IsCheckable = true,
|
||||
IsEnabled = item.IsEnabled
|
||||
};
|
||||
|
||||
menuItem.Click += delegate
|
||||
{
|
||||
_buttonOverflow.IsChecked = false;
|
||||
|
||||
var list = GetActualList();
|
||||
if (list == null) return;
|
||||
|
||||
var actualItem = ItemContainerGenerator.ItemFromContainer(item);
|
||||
if (actualItem == null) return;
|
||||
|
||||
var index = list.IndexOf(actualItem);
|
||||
if (index >= _itemShowCount)
|
||||
{
|
||||
list.Remove(actualItem);
|
||||
list.Insert(0, actualItem);
|
||||
HeaderPanel.SetValue(TabPanel.FluidMoveDurationPropertyKey,
|
||||
IsAnimationEnabled
|
||||
? new Duration(TimeSpan.FromMilliseconds(200))
|
||||
: new Duration(TimeSpan.FromMilliseconds(0)));
|
||||
HeaderPanel.ForceUpdate = true;
|
||||
HeaderPanel.Measure(new Size(HeaderPanel.DesiredSize.Width, ActualHeight));
|
||||
HeaderPanel.ForceUpdate = false;
|
||||
SetCurrentValue(SelectedIndexProperty, ValueBoxes.Int0Box);
|
||||
}
|
||||
|
||||
item.IsSelected = true;
|
||||
};
|
||||
_buttonOverflow.Menu.Items.Add(menuItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal double GetHorizontalOffset() => _scrollViewerOverflow?.CurrentHorizontalOffset ?? 0;
|
||||
|
||||
internal void UpdateScroll() => _scrollViewerOverflow?.RaiseEvent(new MouseWheelEventArgs(Mouse.PrimaryDevice, Environment.TickCount, 0)
|
||||
{
|
||||
RoutedEvent = MouseWheelEvent
|
||||
});
|
||||
|
||||
internal void CloseAllItems() => CloseOtherItems(null);
|
||||
|
||||
internal void CloseOtherItems(TabItem currentItem)
|
||||
{
|
||||
var actualItem = currentItem != null ? ItemContainerGenerator.ItemFromContainer(currentItem) : null;
|
||||
|
||||
var list = GetActualList();
|
||||
if (list == null) return;
|
||||
|
||||
IsInternalAction = true;
|
||||
|
||||
for (var i = 0; i < Items.Count; i++)
|
||||
{
|
||||
var item = list[i];
|
||||
if (!Equals(item, actualItem) && item != null)
|
||||
{
|
||||
var argsClosing = new CancelRoutedEventArgs(TabItem.ClosingEvent, item);
|
||||
|
||||
if (ItemContainerGenerator.ContainerFromItem(item) is not TabItem tabItem) continue;
|
||||
|
||||
tabItem.RaiseEvent(argsClosing);
|
||||
if (argsClosing.Cancel) return;
|
||||
|
||||
tabItem.RaiseEvent(new RoutedEventArgs(TabItem.ClosedEvent, item));
|
||||
list.Remove(item);
|
||||
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
SetCurrentValue(SelectedIndexProperty, Items.Count == 0 ? -1 : 0);
|
||||
}
|
||||
|
||||
internal IList GetActualList()
|
||||
{
|
||||
IList list;
|
||||
if (ItemsSource != null)
|
||||
{
|
||||
list = ItemsSource as IList;
|
||||
}
|
||||
else
|
||||
{
|
||||
list = Items;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
protected override bool IsItemItsOwnContainerOverride(object item) => item is TabItem;
|
||||
|
||||
protected override DependencyObject GetContainerForItemOverride() => new TabItem();
|
||||
}
|
||||
@@ -11,481 +11,480 @@ using HandyControl.Interactivity;
|
||||
using HandyControl.Tools;
|
||||
using HandyControl.Tools.Extension;
|
||||
|
||||
namespace HandyControl.Controls
|
||||
namespace HandyControl.Controls;
|
||||
|
||||
public class TabItem : System.Windows.Controls.TabItem
|
||||
{
|
||||
public class TabItem : System.Windows.Controls.TabItem
|
||||
/// <summary>
|
||||
/// 动画速度
|
||||
/// </summary>
|
||||
private const int AnimationSpeed = 150;
|
||||
|
||||
/// <summary>
|
||||
/// 选项卡是否处于拖动状态
|
||||
/// </summary>
|
||||
private static bool ItemIsDragging;
|
||||
|
||||
/// <summary>
|
||||
/// 选项卡是否等待被拖动
|
||||
/// </summary>
|
||||
private bool _isWaiting;
|
||||
|
||||
/// <summary>
|
||||
/// 拖动中的选项卡坐标
|
||||
/// </summary>
|
||||
private Point _dragPoint;
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标按下时选项卡位置
|
||||
/// </summary>
|
||||
private int _mouseDownIndex;
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标按下时选项卡横向偏移
|
||||
/// </summary>
|
||||
private double _mouseDownOffsetX;
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标按下时的坐标
|
||||
/// </summary>
|
||||
private Point _mouseDownPoint;
|
||||
|
||||
/// <summary>
|
||||
/// 右侧可移动的最大值
|
||||
/// </summary>
|
||||
private double _maxMoveRight;
|
||||
|
||||
/// <summary>
|
||||
/// 左侧可移动的最大值
|
||||
/// </summary>
|
||||
private double _maxMoveLeft;
|
||||
|
||||
/// <summary>
|
||||
/// 选项卡宽度
|
||||
/// </summary>
|
||||
public double ItemWidth { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// 选项卡拖动等待距离(在鼠标移动了超过20个像素无关单位后,选项卡才开始被拖动)
|
||||
/// </summary>
|
||||
private const double WaitLength = 20;
|
||||
|
||||
/// <summary>
|
||||
/// 选项卡是否处于拖动状态
|
||||
/// </summary>
|
||||
private bool _isDragging;
|
||||
|
||||
/// <summary>
|
||||
/// 选项卡是否已经被拖动
|
||||
/// </summary>
|
||||
private bool _isDragged;
|
||||
|
||||
/// <summary>
|
||||
/// 目标横向位移
|
||||
/// </summary>
|
||||
internal double TargetOffsetX { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前编号
|
||||
/// </summary>
|
||||
private int _currentIndex;
|
||||
|
||||
/// <summary>
|
||||
/// 标签容器横向滚动距离
|
||||
/// </summary>
|
||||
private double _scrollHorizontalOffset;
|
||||
|
||||
private TabPanel _tabPanel;
|
||||
|
||||
/// <summary>
|
||||
/// 标签容器
|
||||
/// </summary>
|
||||
internal TabPanel TabPanel
|
||||
{
|
||||
/// <summary>
|
||||
/// 动画速度
|
||||
/// </summary>
|
||||
private const int AnimationSpeed = 150;
|
||||
|
||||
/// <summary>
|
||||
/// 选项卡是否处于拖动状态
|
||||
/// </summary>
|
||||
private static bool ItemIsDragging;
|
||||
|
||||
/// <summary>
|
||||
/// 选项卡是否等待被拖动
|
||||
/// </summary>
|
||||
private bool _isWaiting;
|
||||
|
||||
/// <summary>
|
||||
/// 拖动中的选项卡坐标
|
||||
/// </summary>
|
||||
private Point _dragPoint;
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标按下时选项卡位置
|
||||
/// </summary>
|
||||
private int _mouseDownIndex;
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标按下时选项卡横向偏移
|
||||
/// </summary>
|
||||
private double _mouseDownOffsetX;
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标按下时的坐标
|
||||
/// </summary>
|
||||
private Point _mouseDownPoint;
|
||||
|
||||
/// <summary>
|
||||
/// 右侧可移动的最大值
|
||||
/// </summary>
|
||||
private double _maxMoveRight;
|
||||
|
||||
/// <summary>
|
||||
/// 左侧可移动的最大值
|
||||
/// </summary>
|
||||
private double _maxMoveLeft;
|
||||
|
||||
/// <summary>
|
||||
/// 选项卡宽度
|
||||
/// </summary>
|
||||
public double ItemWidth { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// 选项卡拖动等待距离(在鼠标移动了超过20个像素无关单位后,选项卡才开始被拖动)
|
||||
/// </summary>
|
||||
private const double WaitLength = 20;
|
||||
|
||||
/// <summary>
|
||||
/// 选项卡是否处于拖动状态
|
||||
/// </summary>
|
||||
private bool _isDragging;
|
||||
|
||||
/// <summary>
|
||||
/// 选项卡是否已经被拖动
|
||||
/// </summary>
|
||||
private bool _isDragged;
|
||||
|
||||
/// <summary>
|
||||
/// 目标横向位移
|
||||
/// </summary>
|
||||
internal double TargetOffsetX { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前编号
|
||||
/// </summary>
|
||||
private int _currentIndex;
|
||||
|
||||
/// <summary>
|
||||
/// 标签容器横向滚动距离
|
||||
/// </summary>
|
||||
private double _scrollHorizontalOffset;
|
||||
|
||||
private TabPanel _tabPanel;
|
||||
|
||||
/// <summary>
|
||||
/// 标签容器
|
||||
/// </summary>
|
||||
internal TabPanel TabPanel
|
||||
get
|
||||
{
|
||||
get
|
||||
if (_tabPanel == null && TabControlParent != null)
|
||||
{
|
||||
if (_tabPanel == null && TabControlParent != null)
|
||||
{
|
||||
_tabPanel = TabControlParent.HeaderPanel;
|
||||
}
|
||||
|
||||
return _tabPanel;
|
||||
}
|
||||
set => _tabPanel = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当前编号
|
||||
/// </summary>
|
||||
internal int CurrentIndex
|
||||
{
|
||||
get => _currentIndex;
|
||||
set
|
||||
{
|
||||
if (_currentIndex == value || value < 0) return;
|
||||
var oldIndex = _currentIndex;
|
||||
_currentIndex = value;
|
||||
UpdateItemOffsetX(oldIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示关闭按钮
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShowCloseButtonProperty =
|
||||
TabControl.ShowCloseButtonProperty.AddOwner(typeof(TabItem));
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示关闭按钮
|
||||
/// </summary>
|
||||
public bool ShowCloseButton
|
||||
{
|
||||
get => (bool) GetValue(ShowCloseButtonProperty);
|
||||
set => SetValue(ShowCloseButtonProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
public static void SetShowCloseButton(DependencyObject element, bool value)
|
||||
=> element.SetValue(ShowCloseButtonProperty, ValueBoxes.BooleanBox(value));
|
||||
|
||||
public static bool GetShowCloseButton(DependencyObject element)
|
||||
=> (bool) element.GetValue(ShowCloseButtonProperty);
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示上下文菜单
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShowContextMenuProperty =
|
||||
TabControl.ShowContextMenuProperty.AddOwner(typeof(TabItem), new FrameworkPropertyMetadata(OnShowContextMenuChanged));
|
||||
|
||||
private static void OnShowContextMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var ctl = (TabItem) d;
|
||||
if (ctl.Menu != null)
|
||||
{
|
||||
var show = (bool) e.NewValue;
|
||||
ctl.Menu.IsEnabled = show;
|
||||
ctl.Menu.Show(show);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示上下文菜单
|
||||
/// </summary>
|
||||
public bool ShowContextMenu
|
||||
{
|
||||
get => (bool) GetValue(ShowContextMenuProperty);
|
||||
set => SetValue(ShowContextMenuProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
public static void SetShowContextMenu(DependencyObject element, bool value)
|
||||
=> element.SetValue(ShowContextMenuProperty, ValueBoxes.BooleanBox(value));
|
||||
|
||||
public static bool GetShowContextMenu(DependencyObject element)
|
||||
=> (bool) element.GetValue(ShowContextMenuProperty);
|
||||
|
||||
public static readonly DependencyProperty MenuProperty = DependencyProperty.Register(
|
||||
"Menu", typeof(ContextMenu), typeof(TabItem), new PropertyMetadata(default(ContextMenu), OnMenuChanged));
|
||||
|
||||
private static void OnMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var ctl = (TabItem) d;
|
||||
ctl.OnMenuChanged(e.NewValue as ContextMenu);
|
||||
}
|
||||
|
||||
private void OnMenuChanged(ContextMenu menu)
|
||||
{
|
||||
if (IsLoaded && menu != null)
|
||||
{
|
||||
var parent = TabControlParent;
|
||||
if (parent == null) return;
|
||||
|
||||
var item = parent.ItemContainerGenerator.ItemFromContainer(this);
|
||||
|
||||
menu.DataContext = item;
|
||||
menu.SetBinding(IsEnabledProperty, new Binding(ShowContextMenuProperty.Name)
|
||||
{
|
||||
Source = this
|
||||
});
|
||||
menu.SetBinding(VisibilityProperty, new Binding(ShowContextMenuProperty.Name)
|
||||
{
|
||||
Source = this,
|
||||
Converter = ResourceHelper.GetResourceInternal<IValueConverter>(ResourceToken.Boolean2VisibilityConverter)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public ContextMenu Menu
|
||||
{
|
||||
get => (ContextMenu) GetValue(MenuProperty);
|
||||
set => SetValue(MenuProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新选项卡横向偏移
|
||||
/// </summary>
|
||||
/// <param name="oldIndex"></param>
|
||||
private void UpdateItemOffsetX(int oldIndex)
|
||||
{
|
||||
if (!_isDragging || CurrentIndex >= TabPanel.ItemDic.Count)
|
||||
{
|
||||
return;
|
||||
_tabPanel = TabControlParent.HeaderPanel;
|
||||
}
|
||||
|
||||
var moveItem = TabPanel.ItemDic[CurrentIndex];
|
||||
moveItem.CurrentIndex -= CurrentIndex - oldIndex;
|
||||
var offsetX = moveItem.TargetOffsetX;
|
||||
var resultX = offsetX + (oldIndex - CurrentIndex) * ItemWidth;
|
||||
TabPanel.ItemDic[CurrentIndex] = this;
|
||||
TabPanel.ItemDic[moveItem.CurrentIndex] = moveItem;
|
||||
moveItem.CreateAnimation(offsetX, resultX);
|
||||
return _tabPanel;
|
||||
}
|
||||
set => _tabPanel = value;
|
||||
}
|
||||
|
||||
public TabItem()
|
||||
/// <summary>
|
||||
/// 当前编号
|
||||
/// </summary>
|
||||
internal int CurrentIndex
|
||||
{
|
||||
get => _currentIndex;
|
||||
set
|
||||
{
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.Close, (s, e) => Close()));
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.CloseAll,
|
||||
(s, e) => { TabControlParent.CloseAllItems(); }));
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.CloseOther,
|
||||
(s, e) => { TabControlParent.CloseOtherItems(this); }));
|
||||
|
||||
Loaded += (s, e) => OnMenuChanged(Menu);
|
||||
if (_currentIndex == value || value < 0) return;
|
||||
var oldIndex = _currentIndex;
|
||||
_currentIndex = value;
|
||||
UpdateItemOffsetX(oldIndex);
|
||||
}
|
||||
}
|
||||
|
||||
private TabControl TabControlParent => ItemsControl.ItemsControlFromItemContainer(this) as TabControl;
|
||||
/// <summary>
|
||||
/// 是否显示关闭按钮
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShowCloseButtonProperty =
|
||||
TabControl.ShowCloseButtonProperty.AddOwner(typeof(TabItem));
|
||||
|
||||
protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
|
||||
/// <summary>
|
||||
/// 是否显示关闭按钮
|
||||
/// </summary>
|
||||
public bool ShowCloseButton
|
||||
{
|
||||
get => (bool) GetValue(ShowCloseButtonProperty);
|
||||
set => SetValue(ShowCloseButtonProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
public static void SetShowCloseButton(DependencyObject element, bool value)
|
||||
=> element.SetValue(ShowCloseButtonProperty, ValueBoxes.BooleanBox(value));
|
||||
|
||||
public static bool GetShowCloseButton(DependencyObject element)
|
||||
=> (bool) element.GetValue(ShowCloseButtonProperty);
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示上下文菜单
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShowContextMenuProperty =
|
||||
TabControl.ShowContextMenuProperty.AddOwner(typeof(TabItem), new FrameworkPropertyMetadata(OnShowContextMenuChanged));
|
||||
|
||||
private static void OnShowContextMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var ctl = (TabItem) d;
|
||||
if (ctl.Menu != null)
|
||||
{
|
||||
base.OnMouseRightButtonDown(e);
|
||||
|
||||
if (VisualTreeHelper.HitTest(this, e.GetPosition(this)) == null) return;
|
||||
IsSelected = true;
|
||||
Focus();
|
||||
var show = (bool) e.NewValue;
|
||||
ctl.Menu.IsEnabled = show;
|
||||
ctl.Menu.Show(show);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnHeaderChanged(object oldHeader, object newHeader)
|
||||
{
|
||||
base.OnHeaderChanged(oldHeader, newHeader);
|
||||
/// <summary>
|
||||
/// 是否显示上下文菜单
|
||||
/// </summary>
|
||||
public bool ShowContextMenu
|
||||
{
|
||||
get => (bool) GetValue(ShowContextMenuProperty);
|
||||
set => SetValue(ShowContextMenuProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
if (TabPanel != null)
|
||||
{
|
||||
TabPanel.ForceUpdate = true;
|
||||
InvalidateMeasure();
|
||||
TabPanel.ForceUpdate = true;
|
||||
}
|
||||
}
|
||||
public static void SetShowContextMenu(DependencyObject element, bool value)
|
||||
=> element.SetValue(ShowContextMenuProperty, ValueBoxes.BooleanBox(value));
|
||||
|
||||
internal void Close()
|
||||
public static bool GetShowContextMenu(DependencyObject element)
|
||||
=> (bool) element.GetValue(ShowContextMenuProperty);
|
||||
|
||||
public static readonly DependencyProperty MenuProperty = DependencyProperty.Register(
|
||||
"Menu", typeof(ContextMenu), typeof(TabItem), new PropertyMetadata(default(ContextMenu), OnMenuChanged));
|
||||
|
||||
private static void OnMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var ctl = (TabItem) d;
|
||||
ctl.OnMenuChanged(e.NewValue as ContextMenu);
|
||||
}
|
||||
|
||||
private void OnMenuChanged(ContextMenu menu)
|
||||
{
|
||||
if (IsLoaded && menu != null)
|
||||
{
|
||||
var parent = TabControlParent;
|
||||
if (parent == null) return;
|
||||
|
||||
var item = parent.ItemContainerGenerator.ItemFromContainer(this);
|
||||
|
||||
var argsClosing = new CancelRoutedEventArgs(ClosingEvent, item);
|
||||
RaiseEvent(argsClosing);
|
||||
if (argsClosing.Cancel) return;
|
||||
menu.DataContext = item;
|
||||
menu.SetBinding(IsEnabledProperty, new Binding(ShowContextMenuProperty.Name)
|
||||
{
|
||||
Source = this
|
||||
});
|
||||
menu.SetBinding(VisibilityProperty, new Binding(ShowContextMenuProperty.Name)
|
||||
{
|
||||
Source = this,
|
||||
Converter = ResourceHelper.GetResourceInternal<IValueConverter>(ResourceToken.Boolean2VisibilityConverter)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
TabPanel.SetValue(TabPanel.FluidMoveDurationPropertyKey, parent.IsAnimationEnabled
|
||||
? new Duration(TimeSpan.FromMilliseconds(200))
|
||||
: new Duration(TimeSpan.FromMilliseconds(1)));
|
||||
public ContextMenu Menu
|
||||
{
|
||||
get => (ContextMenu) GetValue(MenuProperty);
|
||||
set => SetValue(MenuProperty, value);
|
||||
}
|
||||
|
||||
parent.IsInternalAction = true;
|
||||
RaiseEvent(new RoutedEventArgs(ClosedEvent, item));
|
||||
|
||||
var list = parent.GetActualList();
|
||||
list?.Remove(item);
|
||||
/// <summary>
|
||||
/// 更新选项卡横向偏移
|
||||
/// </summary>
|
||||
/// <param name="oldIndex"></param>
|
||||
private void UpdateItemOffsetX(int oldIndex)
|
||||
{
|
||||
if (!_isDragging || CurrentIndex >= TabPanel.ItemDic.Count)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
|
||||
{
|
||||
base.OnMouseLeftButtonDown(e);
|
||||
var moveItem = TabPanel.ItemDic[CurrentIndex];
|
||||
moveItem.CurrentIndex -= CurrentIndex - oldIndex;
|
||||
var offsetX = moveItem.TargetOffsetX;
|
||||
var resultX = offsetX + (oldIndex - CurrentIndex) * ItemWidth;
|
||||
TabPanel.ItemDic[CurrentIndex] = this;
|
||||
TabPanel.ItemDic[moveItem.CurrentIndex] = moveItem;
|
||||
moveItem.CreateAnimation(offsetX, resultX);
|
||||
}
|
||||
|
||||
if (VisualTreeHelper.HitTest(this, e.GetPosition(this)) == null) return;
|
||||
public TabItem()
|
||||
{
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.Close, (s, e) => Close()));
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.CloseAll,
|
||||
(s, e) => { TabControlParent.CloseAllItems(); }));
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.CloseOther,
|
||||
(s, e) => { TabControlParent.CloseOtherItems(this); }));
|
||||
|
||||
Loaded += (s, e) => OnMenuChanged(Menu);
|
||||
}
|
||||
|
||||
private TabControl TabControlParent => ItemsControl.ItemsControlFromItemContainer(this) as TabControl;
|
||||
|
||||
protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
|
||||
{
|
||||
base.OnMouseRightButtonDown(e);
|
||||
|
||||
if (VisualTreeHelper.HitTest(this, e.GetPosition(this)) == null) return;
|
||||
IsSelected = true;
|
||||
Focus();
|
||||
}
|
||||
|
||||
protected override void OnHeaderChanged(object oldHeader, object newHeader)
|
||||
{
|
||||
base.OnHeaderChanged(oldHeader, newHeader);
|
||||
|
||||
if (TabPanel != null)
|
||||
{
|
||||
TabPanel.ForceUpdate = true;
|
||||
InvalidateMeasure();
|
||||
TabPanel.ForceUpdate = true;
|
||||
}
|
||||
}
|
||||
|
||||
internal void Close()
|
||||
{
|
||||
var parent = TabControlParent;
|
||||
if (parent == null) return;
|
||||
|
||||
var item = parent.ItemContainerGenerator.ItemFromContainer(this);
|
||||
|
||||
var argsClosing = new CancelRoutedEventArgs(ClosingEvent, item);
|
||||
RaiseEvent(argsClosing);
|
||||
if (argsClosing.Cancel) return;
|
||||
|
||||
TabPanel.SetValue(TabPanel.FluidMoveDurationPropertyKey, parent.IsAnimationEnabled
|
||||
? new Duration(TimeSpan.FromMilliseconds(200))
|
||||
: new Duration(TimeSpan.FromMilliseconds(1)));
|
||||
|
||||
parent.IsInternalAction = true;
|
||||
RaiseEvent(new RoutedEventArgs(ClosedEvent, item));
|
||||
|
||||
var list = parent.GetActualList();
|
||||
list?.Remove(item);
|
||||
}
|
||||
|
||||
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
|
||||
{
|
||||
base.OnMouseLeftButtonDown(e);
|
||||
|
||||
if (VisualTreeHelper.HitTest(this, e.GetPosition(this)) == null) return;
|
||||
var parent = TabControlParent;
|
||||
if (parent == null) return;
|
||||
|
||||
if (parent.IsDraggable && !ItemIsDragging && !_isDragging)
|
||||
{
|
||||
parent.UpdateScroll();
|
||||
TabPanel.SetValue(TabPanel.FluidMoveDurationPropertyKey, new Duration(TimeSpan.FromSeconds(0)));
|
||||
_mouseDownOffsetX = RenderTransform.Value.OffsetX;
|
||||
_scrollHorizontalOffset = parent.GetHorizontalOffset();
|
||||
var mx = TranslatePoint(new Point(), parent).X + _scrollHorizontalOffset;
|
||||
_mouseDownIndex = CalLocationIndex(mx);
|
||||
var subIndex = _mouseDownIndex - CalLocationIndex(_scrollHorizontalOffset);
|
||||
_maxMoveLeft = -subIndex * ItemWidth;
|
||||
_maxMoveRight = parent.ActualWidth - ActualWidth + _maxMoveLeft;
|
||||
|
||||
_isDragging = true;
|
||||
ItemIsDragging = true;
|
||||
_isWaiting = true;
|
||||
_dragPoint = e.GetPosition(parent);
|
||||
_dragPoint = new Point(_dragPoint.X + _scrollHorizontalOffset, _dragPoint.Y);
|
||||
_mouseDownPoint = _dragPoint;
|
||||
CaptureMouse();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnMouseMove(MouseEventArgs e)
|
||||
{
|
||||
base.OnMouseMove(e);
|
||||
|
||||
if (ItemIsDragging && _isDragging)
|
||||
{
|
||||
var parent = TabControlParent;
|
||||
if (parent == null) return;
|
||||
|
||||
if (parent.IsDraggable && !ItemIsDragging && !_isDragging)
|
||||
{
|
||||
parent.UpdateScroll();
|
||||
TabPanel.SetValue(TabPanel.FluidMoveDurationPropertyKey, new Duration(TimeSpan.FromSeconds(0)));
|
||||
_mouseDownOffsetX = RenderTransform.Value.OffsetX;
|
||||
_scrollHorizontalOffset = parent.GetHorizontalOffset();
|
||||
var mx = TranslatePoint(new Point(), parent).X + _scrollHorizontalOffset;
|
||||
_mouseDownIndex = CalLocationIndex(mx);
|
||||
var subIndex = _mouseDownIndex - CalLocationIndex(_scrollHorizontalOffset);
|
||||
_maxMoveLeft = -subIndex * ItemWidth;
|
||||
_maxMoveRight = parent.ActualWidth - ActualWidth + _maxMoveLeft;
|
||||
var subX = TranslatePoint(new Point(), parent).X + _scrollHorizontalOffset;
|
||||
CurrentIndex = CalLocationIndex(subX);
|
||||
|
||||
_isDragging = true;
|
||||
ItemIsDragging = true;
|
||||
_isWaiting = true;
|
||||
_dragPoint = e.GetPosition(parent);
|
||||
_dragPoint = new Point(_dragPoint.X + _scrollHorizontalOffset, _dragPoint.Y);
|
||||
_mouseDownPoint = _dragPoint;
|
||||
CaptureMouse();
|
||||
var p = e.GetPosition(parent);
|
||||
p = new Point(p.X + _scrollHorizontalOffset, p.Y);
|
||||
|
||||
var subLeft = p.X - _dragPoint.X;
|
||||
var totalLeft = p.X - _mouseDownPoint.X;
|
||||
|
||||
if (Math.Abs(subLeft) <= WaitLength && _isWaiting) return;
|
||||
|
||||
_isWaiting = false;
|
||||
_isDragged = true;
|
||||
|
||||
var left = subLeft + RenderTransform.Value.OffsetX;
|
||||
if (totalLeft < _maxMoveLeft)
|
||||
{
|
||||
left = _maxMoveLeft + _mouseDownOffsetX;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnMouseMove(MouseEventArgs e)
|
||||
{
|
||||
base.OnMouseMove(e);
|
||||
|
||||
if (ItemIsDragging && _isDragging)
|
||||
else if (totalLeft > _maxMoveRight)
|
||||
{
|
||||
var parent = TabControlParent;
|
||||
if (parent == null) return;
|
||||
|
||||
var subX = TranslatePoint(new Point(), parent).X + _scrollHorizontalOffset;
|
||||
CurrentIndex = CalLocationIndex(subX);
|
||||
|
||||
var p = e.GetPosition(parent);
|
||||
p = new Point(p.X + _scrollHorizontalOffset, p.Y);
|
||||
|
||||
var subLeft = p.X - _dragPoint.X;
|
||||
var totalLeft = p.X - _mouseDownPoint.X;
|
||||
|
||||
if (Math.Abs(subLeft) <= WaitLength && _isWaiting) return;
|
||||
|
||||
_isWaiting = false;
|
||||
_isDragged = true;
|
||||
|
||||
var left = subLeft + RenderTransform.Value.OffsetX;
|
||||
if (totalLeft < _maxMoveLeft)
|
||||
{
|
||||
left = _maxMoveLeft + _mouseDownOffsetX;
|
||||
}
|
||||
else if (totalLeft > _maxMoveRight)
|
||||
{
|
||||
left = _maxMoveRight + _mouseDownOffsetX;
|
||||
}
|
||||
|
||||
var t = new TranslateTransform(left, 0);
|
||||
RenderTransform = t;
|
||||
_dragPoint = p;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
|
||||
{
|
||||
base.OnMouseLeftButtonUp(e);
|
||||
|
||||
ReleaseMouseCapture();
|
||||
|
||||
if (_isDragged)
|
||||
{
|
||||
var parent = TabControlParent;
|
||||
if (parent == null) return;
|
||||
|
||||
var subX = TranslatePoint(new Point(), parent).X + _scrollHorizontalOffset;
|
||||
var index = CalLocationIndex(subX);
|
||||
var left = index * ItemWidth;
|
||||
var offsetX = RenderTransform.Value.OffsetX;
|
||||
CreateAnimation(offsetX, offsetX - subX + left, index);
|
||||
left = _maxMoveRight + _mouseDownOffsetX;
|
||||
}
|
||||
|
||||
_isDragging = false;
|
||||
ItemIsDragging = false;
|
||||
_isDragged = false;
|
||||
}
|
||||
|
||||
protected override void OnMouseDown(MouseButtonEventArgs e)
|
||||
{
|
||||
if (e.ChangedButton == MouseButton.Middle && e.ButtonState == MouseButtonState.Pressed)
|
||||
{
|
||||
if (ShowCloseButton || ShowContextMenu)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建动画
|
||||
/// </summary>
|
||||
internal void CreateAnimation(double offsetX, double resultX, int index = -1)
|
||||
{
|
||||
var parent = TabControlParent;
|
||||
|
||||
void AnimationCompleted()
|
||||
{
|
||||
RenderTransform = new TranslateTransform(resultX, 0);
|
||||
if (index == -1) return;
|
||||
|
||||
var list = parent.GetActualList();
|
||||
if (list == null) return;
|
||||
|
||||
var item = parent.ItemContainerGenerator.ItemFromContainer(this);
|
||||
if (item == null) return;
|
||||
|
||||
TabPanel.CanUpdate = false;
|
||||
parent.IsInternalAction = true;
|
||||
|
||||
list.Remove(item);
|
||||
parent.IsInternalAction = true;
|
||||
list.Insert(index, item);
|
||||
_tabPanel.SetValue(TabPanel.FluidMoveDurationPropertyKey, new Duration(TimeSpan.FromMilliseconds(0)));
|
||||
TabPanel.CanUpdate = true;
|
||||
TabPanel.ForceUpdate = true;
|
||||
TabPanel.Measure(new Size(TabPanel.DesiredSize.Width, ActualHeight));
|
||||
TabPanel.ForceUpdate = false;
|
||||
|
||||
Focus();
|
||||
IsSelected = true;
|
||||
|
||||
if (!IsMouseCaptured)
|
||||
{
|
||||
parent.SetCurrentValue(Selector.SelectedIndexProperty, _currentIndex);
|
||||
}
|
||||
}
|
||||
|
||||
TargetOffsetX = resultX;
|
||||
if (!parent.IsAnimationEnabled)
|
||||
{
|
||||
AnimationCompleted();
|
||||
return;
|
||||
}
|
||||
|
||||
var animation = AnimationHelper.CreateAnimation(resultX, AnimationSpeed);
|
||||
animation.FillBehavior = FillBehavior.Stop;
|
||||
animation.Completed += (s1, e1) => AnimationCompleted();
|
||||
var f = new TranslateTransform(offsetX, 0);
|
||||
RenderTransform = f;
|
||||
f.BeginAnimation(TranslateTransform.XProperty, animation, HandoffBehavior.Compose);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算选项卡当前合适的位置编号
|
||||
/// </summary>
|
||||
/// <param name="left"></param>
|
||||
/// <returns></returns>
|
||||
private int CalLocationIndex(double left)
|
||||
{
|
||||
if (_isWaiting)
|
||||
{
|
||||
return CurrentIndex;
|
||||
}
|
||||
|
||||
var maxIndex = TabControlParent.Items.Count - 1;
|
||||
var div = (int) (left / ItemWidth);
|
||||
var rest = left % ItemWidth;
|
||||
var result = rest / ItemWidth > .5 ? div + 1 : div;
|
||||
|
||||
return result > maxIndex ? maxIndex : result;
|
||||
}
|
||||
|
||||
public static readonly RoutedEvent ClosingEvent = EventManager.RegisterRoutedEvent("Closing", RoutingStrategy.Bubble, typeof(EventHandler), typeof(TabItem));
|
||||
|
||||
public event EventHandler Closing
|
||||
{
|
||||
add => AddHandler(ClosingEvent, value);
|
||||
remove => RemoveHandler(ClosingEvent, value);
|
||||
}
|
||||
|
||||
public static readonly RoutedEvent ClosedEvent = EventManager.RegisterRoutedEvent("Closed", RoutingStrategy.Bubble, typeof(EventHandler), typeof(TabItem));
|
||||
|
||||
public event EventHandler Closed
|
||||
{
|
||||
add => AddHandler(ClosedEvent, value);
|
||||
remove => RemoveHandler(ClosedEvent, value);
|
||||
var t = new TranslateTransform(left, 0);
|
||||
RenderTransform = t;
|
||||
_dragPoint = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
|
||||
{
|
||||
base.OnMouseLeftButtonUp(e);
|
||||
|
||||
ReleaseMouseCapture();
|
||||
|
||||
if (_isDragged)
|
||||
{
|
||||
var parent = TabControlParent;
|
||||
if (parent == null) return;
|
||||
|
||||
var subX = TranslatePoint(new Point(), parent).X + _scrollHorizontalOffset;
|
||||
var index = CalLocationIndex(subX);
|
||||
var left = index * ItemWidth;
|
||||
var offsetX = RenderTransform.Value.OffsetX;
|
||||
CreateAnimation(offsetX, offsetX - subX + left, index);
|
||||
}
|
||||
|
||||
_isDragging = false;
|
||||
ItemIsDragging = false;
|
||||
_isDragged = false;
|
||||
}
|
||||
|
||||
protected override void OnMouseDown(MouseButtonEventArgs e)
|
||||
{
|
||||
if (e.ChangedButton == MouseButton.Middle && e.ButtonState == MouseButtonState.Pressed)
|
||||
{
|
||||
if (ShowCloseButton || ShowContextMenu)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建动画
|
||||
/// </summary>
|
||||
internal void CreateAnimation(double offsetX, double resultX, int index = -1)
|
||||
{
|
||||
var parent = TabControlParent;
|
||||
|
||||
void AnimationCompleted()
|
||||
{
|
||||
RenderTransform = new TranslateTransform(resultX, 0);
|
||||
if (index == -1) return;
|
||||
|
||||
var list = parent.GetActualList();
|
||||
if (list == null) return;
|
||||
|
||||
var item = parent.ItemContainerGenerator.ItemFromContainer(this);
|
||||
if (item == null) return;
|
||||
|
||||
TabPanel.CanUpdate = false;
|
||||
parent.IsInternalAction = true;
|
||||
|
||||
list.Remove(item);
|
||||
parent.IsInternalAction = true;
|
||||
list.Insert(index, item);
|
||||
_tabPanel.SetValue(TabPanel.FluidMoveDurationPropertyKey, new Duration(TimeSpan.FromMilliseconds(0)));
|
||||
TabPanel.CanUpdate = true;
|
||||
TabPanel.ForceUpdate = true;
|
||||
TabPanel.Measure(new Size(TabPanel.DesiredSize.Width, ActualHeight));
|
||||
TabPanel.ForceUpdate = false;
|
||||
|
||||
Focus();
|
||||
IsSelected = true;
|
||||
|
||||
if (!IsMouseCaptured)
|
||||
{
|
||||
parent.SetCurrentValue(Selector.SelectedIndexProperty, _currentIndex);
|
||||
}
|
||||
}
|
||||
|
||||
TargetOffsetX = resultX;
|
||||
if (!parent.IsAnimationEnabled)
|
||||
{
|
||||
AnimationCompleted();
|
||||
return;
|
||||
}
|
||||
|
||||
var animation = AnimationHelper.CreateAnimation(resultX, AnimationSpeed);
|
||||
animation.FillBehavior = FillBehavior.Stop;
|
||||
animation.Completed += (s1, e1) => AnimationCompleted();
|
||||
var f = new TranslateTransform(offsetX, 0);
|
||||
RenderTransform = f;
|
||||
f.BeginAnimation(TranslateTransform.XProperty, animation, HandoffBehavior.Compose);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算选项卡当前合适的位置编号
|
||||
/// </summary>
|
||||
/// <param name="left"></param>
|
||||
/// <returns></returns>
|
||||
private int CalLocationIndex(double left)
|
||||
{
|
||||
if (_isWaiting)
|
||||
{
|
||||
return CurrentIndex;
|
||||
}
|
||||
|
||||
var maxIndex = TabControlParent.Items.Count - 1;
|
||||
var div = (int) (left / ItemWidth);
|
||||
var rest = left % ItemWidth;
|
||||
var result = rest / ItemWidth > .5 ? div + 1 : div;
|
||||
|
||||
return result > maxIndex ? maxIndex : result;
|
||||
}
|
||||
|
||||
public static readonly RoutedEvent ClosingEvent = EventManager.RegisterRoutedEvent("Closing", RoutingStrategy.Bubble, typeof(EventHandler), typeof(TabItem));
|
||||
|
||||
public event EventHandler Closing
|
||||
{
|
||||
add => AddHandler(ClosingEvent, value);
|
||||
remove => RemoveHandler(ClosingEvent, value);
|
||||
}
|
||||
|
||||
public static readonly RoutedEvent ClosedEvent = EventManager.RegisterRoutedEvent("Closed", RoutingStrategy.Bubble, typeof(EventHandler), typeof(TabItem));
|
||||
|
||||
public event EventHandler Closed
|
||||
{
|
||||
add => AddHandler(ClosedEvent, value);
|
||||
remove => RemoveHandler(ClosedEvent, value);
|
||||
}
|
||||
}
|
||||
@@ -6,174 +6,173 @@ using System.Windows.Media;
|
||||
using HandyControl.Data;
|
||||
using HandyControl.Tools;
|
||||
|
||||
namespace HandyControl.Controls
|
||||
namespace HandyControl.Controls;
|
||||
|
||||
public class TabPanel : Panel
|
||||
{
|
||||
public class TabPanel : Panel
|
||||
private int _itemCount;
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以更新
|
||||
/// </summary>
|
||||
internal bool CanUpdate = true;
|
||||
|
||||
/// <summary>
|
||||
/// 选项卡字典
|
||||
/// </summary>
|
||||
internal Dictionary<int, TabItem> ItemDic = new();
|
||||
|
||||
public static readonly DependencyPropertyKey FluidMoveDurationPropertyKey =
|
||||
DependencyProperty.RegisterReadOnly("FluidMoveDuration", typeof(Duration), typeof(TabPanel),
|
||||
new PropertyMetadata(new Duration(TimeSpan.FromMilliseconds(0))));
|
||||
|
||||
/// <summary>
|
||||
/// 流式行为持续时间
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty FluidMoveDurationProperty =
|
||||
FluidMoveDurationPropertyKey.DependencyProperty;
|
||||
|
||||
/// <summary>
|
||||
/// 流式行为持续时间
|
||||
/// </summary>
|
||||
public Duration FluidMoveDuration
|
||||
{
|
||||
private int _itemCount;
|
||||
get => (Duration) GetValue(FluidMoveDurationProperty);
|
||||
set => SetValue(FluidMoveDurationProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以更新
|
||||
/// </summary>
|
||||
internal bool CanUpdate = true;
|
||||
/// <summary>
|
||||
/// 是否将标签填充
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsTabFillEnabledProperty = DependencyProperty.Register(
|
||||
"IsTabFillEnabled", typeof(bool), typeof(TabPanel), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
/// <summary>
|
||||
/// 选项卡字典
|
||||
/// </summary>
|
||||
internal Dictionary<int, TabItem> ItemDic = new();
|
||||
/// <summary>
|
||||
/// 是否将标签填充
|
||||
/// </summary>
|
||||
public bool IsTabFillEnabled
|
||||
{
|
||||
get => (bool) GetValue(IsTabFillEnabledProperty);
|
||||
set => SetValue(IsTabFillEnabledProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
public static readonly DependencyPropertyKey FluidMoveDurationPropertyKey =
|
||||
DependencyProperty.RegisterReadOnly("FluidMoveDuration", typeof(Duration), typeof(TabPanel),
|
||||
new PropertyMetadata(new Duration(TimeSpan.FromMilliseconds(0))));
|
||||
/// <summary>
|
||||
/// 标签宽度
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty TabItemWidthProperty = DependencyProperty.Register(
|
||||
"TabItemWidth", typeof(double), typeof(TabPanel), new PropertyMetadata(200.0));
|
||||
|
||||
/// <summary>
|
||||
/// 流式行为持续时间
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty FluidMoveDurationProperty =
|
||||
FluidMoveDurationPropertyKey.DependencyProperty;
|
||||
/// <summary>
|
||||
/// 标签宽度
|
||||
/// </summary>
|
||||
public double TabItemWidth
|
||||
{
|
||||
get => (double) GetValue(TabItemWidthProperty);
|
||||
set => SetValue(TabItemWidthProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 流式行为持续时间
|
||||
/// </summary>
|
||||
public Duration FluidMoveDuration
|
||||
/// <summary>
|
||||
/// 标签高度
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty TabItemHeightProperty = DependencyProperty.Register(
|
||||
"TabItemHeight", typeof(double), typeof(TabPanel), new PropertyMetadata(30.0));
|
||||
|
||||
/// <summary>
|
||||
/// 标签高度
|
||||
/// </summary>
|
||||
public double TabItemHeight
|
||||
{
|
||||
get => (double) GetValue(TabItemHeightProperty);
|
||||
set => SetValue(TabItemHeightProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以强制更新
|
||||
/// </summary>
|
||||
internal bool ForceUpdate;
|
||||
|
||||
private Size _oldSize;
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经加载
|
||||
/// </summary>
|
||||
private bool _isLoaded;
|
||||
|
||||
protected override Size MeasureOverride(Size constraint)
|
||||
{
|
||||
if ((_itemCount == InternalChildren.Count || !CanUpdate) && !ForceUpdate && !IsTabFillEnabled) return _oldSize;
|
||||
constraint.Height = TabItemHeight;
|
||||
_itemCount = InternalChildren.Count;
|
||||
|
||||
var size = new Size();
|
||||
|
||||
ItemDic.Clear();
|
||||
|
||||
var count = InternalChildren.Count;
|
||||
if (count == 0)
|
||||
{
|
||||
get => (Duration) GetValue(FluidMoveDurationProperty);
|
||||
set => SetValue(FluidMoveDurationProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否将标签填充
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsTabFillEnabledProperty = DependencyProperty.Register(
|
||||
"IsTabFillEnabled", typeof(bool), typeof(TabPanel), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
/// <summary>
|
||||
/// 是否将标签填充
|
||||
/// </summary>
|
||||
public bool IsTabFillEnabled
|
||||
{
|
||||
get => (bool) GetValue(IsTabFillEnabledProperty);
|
||||
set => SetValue(IsTabFillEnabledProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标签宽度
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty TabItemWidthProperty = DependencyProperty.Register(
|
||||
"TabItemWidth", typeof(double), typeof(TabPanel), new PropertyMetadata(200.0));
|
||||
|
||||
/// <summary>
|
||||
/// 标签宽度
|
||||
/// </summary>
|
||||
public double TabItemWidth
|
||||
{
|
||||
get => (double) GetValue(TabItemWidthProperty);
|
||||
set => SetValue(TabItemWidthProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标签高度
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty TabItemHeightProperty = DependencyProperty.Register(
|
||||
"TabItemHeight", typeof(double), typeof(TabPanel), new PropertyMetadata(30.0));
|
||||
|
||||
/// <summary>
|
||||
/// 标签高度
|
||||
/// </summary>
|
||||
public double TabItemHeight
|
||||
{
|
||||
get => (double) GetValue(TabItemHeightProperty);
|
||||
set => SetValue(TabItemHeightProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以强制更新
|
||||
/// </summary>
|
||||
internal bool ForceUpdate;
|
||||
|
||||
private Size _oldSize;
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经加载
|
||||
/// </summary>
|
||||
private bool _isLoaded;
|
||||
|
||||
protected override Size MeasureOverride(Size constraint)
|
||||
{
|
||||
if ((_itemCount == InternalChildren.Count || !CanUpdate) && !ForceUpdate && !IsTabFillEnabled) return _oldSize;
|
||||
constraint.Height = TabItemHeight;
|
||||
_itemCount = InternalChildren.Count;
|
||||
|
||||
var size = new Size();
|
||||
|
||||
ItemDic.Clear();
|
||||
|
||||
var count = InternalChildren.Count;
|
||||
if (count == 0)
|
||||
{
|
||||
_oldSize = new Size();
|
||||
return _oldSize;
|
||||
}
|
||||
constraint.Width += InternalChildren.Count;
|
||||
|
||||
var itemWidth = .0;
|
||||
var arr = new int[count];
|
||||
|
||||
if (!IsTabFillEnabled)
|
||||
{
|
||||
itemWidth = TabItemWidth;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (TemplatedParent is TabControl tabControl)
|
||||
{
|
||||
arr = ArithmeticHelper.DivideInt2Arr((int) tabControl.ActualWidth + InternalChildren.Count, count);
|
||||
}
|
||||
}
|
||||
|
||||
for (var index = 0; index < count; index++)
|
||||
{
|
||||
if (IsTabFillEnabled)
|
||||
{
|
||||
itemWidth = arr[index];
|
||||
}
|
||||
if (InternalChildren[index] is TabItem tabItem)
|
||||
{
|
||||
tabItem.RenderTransform = new TranslateTransform();
|
||||
tabItem.MaxWidth = itemWidth;
|
||||
var rect = new Rect
|
||||
{
|
||||
X = size.Width - tabItem.BorderThickness.Left,
|
||||
Width = itemWidth,
|
||||
Height = TabItemHeight
|
||||
};
|
||||
tabItem.Arrange(rect);
|
||||
tabItem.ItemWidth = itemWidth - tabItem.BorderThickness.Left;
|
||||
tabItem.CurrentIndex = index;
|
||||
tabItem.TargetOffsetX = 0;
|
||||
ItemDic[index] = tabItem;
|
||||
size.Width += tabItem.ItemWidth;
|
||||
}
|
||||
}
|
||||
size.Height = constraint.Height;
|
||||
_oldSize = size;
|
||||
_oldSize = new Size();
|
||||
return _oldSize;
|
||||
}
|
||||
constraint.Width += InternalChildren.Count;
|
||||
|
||||
public TabPanel()
|
||||
var itemWidth = .0;
|
||||
var arr = new int[count];
|
||||
|
||||
if (!IsTabFillEnabled)
|
||||
{
|
||||
Loaded += (s, e) =>
|
||||
{
|
||||
if (_isLoaded) return;
|
||||
ForceUpdate = true;
|
||||
Measure(new Size(DesiredSize.Width, ActualHeight));
|
||||
ForceUpdate = false;
|
||||
foreach (var item in ItemDic.Values)
|
||||
{
|
||||
item.TabPanel = this;
|
||||
}
|
||||
_isLoaded = true;
|
||||
};
|
||||
itemWidth = TabItemWidth;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (TemplatedParent is TabControl tabControl)
|
||||
{
|
||||
arr = ArithmeticHelper.DivideInt2Arr((int) tabControl.ActualWidth + InternalChildren.Count, count);
|
||||
}
|
||||
}
|
||||
|
||||
for (var index = 0; index < count; index++)
|
||||
{
|
||||
if (IsTabFillEnabled)
|
||||
{
|
||||
itemWidth = arr[index];
|
||||
}
|
||||
if (InternalChildren[index] is TabItem tabItem)
|
||||
{
|
||||
tabItem.RenderTransform = new TranslateTransform();
|
||||
tabItem.MaxWidth = itemWidth;
|
||||
var rect = new Rect
|
||||
{
|
||||
X = size.Width - tabItem.BorderThickness.Left,
|
||||
Width = itemWidth,
|
||||
Height = TabItemHeight
|
||||
};
|
||||
tabItem.Arrange(rect);
|
||||
tabItem.ItemWidth = itemWidth - tabItem.BorderThickness.Left;
|
||||
tabItem.CurrentIndex = index;
|
||||
tabItem.TargetOffsetX = 0;
|
||||
ItemDic[index] = tabItem;
|
||||
size.Width += tabItem.ItemWidth;
|
||||
}
|
||||
}
|
||||
size.Height = constraint.Height;
|
||||
_oldSize = size;
|
||||
return _oldSize;
|
||||
}
|
||||
}
|
||||
|
||||
public TabPanel()
|
||||
{
|
||||
Loaded += (s, e) =>
|
||||
{
|
||||
if (_isLoaded) return;
|
||||
ForceUpdate = true;
|
||||
Measure(new Size(DesiredSize.Width, ActualHeight));
|
||||
ForceUpdate = false;
|
||||
foreach (var item in ItemDic.Values)
|
||||
{
|
||||
item.TabPanel = this;
|
||||
}
|
||||
_isLoaded = true;
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user