Files
Shrlalgo.RvKits/NeuWPF/NeoUI/Assists/TabAssist.cs
2025-08-20 12:10:13 +08:00

109 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace NeumUI.Assists;
/// <summary>
/// TabAssist 类用于在 WPF 应用程序中为 TabControl 提供附加功能。通过依赖属性 UseFadeIn可以控制选项卡切换时是否启用渐变浮现效果。
/// 该类扩展了 DependencyObject因此能够支持数据绑定和样式设置。
/// </summary>
public class TabAssist
{
/// <summary>
/// 获取指定依赖对象是否启用渐变浮现效果。
/// </summary>
/// <param name="obj">要获取渐变浮现效果设置的依赖对象。</param>
/// <returns>如果依赖对象启用了渐变浮现效果,则返回 true否则返回 false。</returns>
public static bool GetUseFadeIn(DependencyObject obj)
{
return (bool)obj.GetValue(UseFadeInProperty);
}
/// <summary>
/// 设置指定依赖对象是否启用渐变浮现效果。
/// </summary>
/// <param name="obj">要设置 UseFadeIn 属性的 DependencyObject。</param>
/// <param name="value">如果为 true则启用渐变浮现效果否则禁用该效果。</param>
public static void SetUseFadeIn(DependencyObject obj, bool value)
{
obj.SetValue(UseFadeInProperty, value);
}
/// <summary>
/// UseFadeInProperty 是一个依赖属性,用于控制 TabControl 在选项卡切换时是否启用渐变浮现效果。通过设置此属性为 true 或 false可以开启或关闭该视觉效果。
/// </summary>
public static readonly DependencyProperty UseFadeInProperty =
DependencyProperty.RegisterAttached("UseFadeIn",
typeof(bool), typeof(TabAssist),
new PropertyMetadata(false, OnUseFadeInChanged));
//切换选项卡时使用渐变浮现效果
private static void OnUseFadeInChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var tabControl = (TabControl)d;
if (!(bool)e.OldValue && (bool)e.NewValue)
{
tabControl.SelectionChanged += TabControl_FadeInEffect;
}
if ((bool)e.OldValue && !(bool)e.NewValue)
{
tabControl.SelectionChanged -= TabControl_FadeInEffect;
}
}
private static void TabControl_FadeInEffect(object sender, SelectionChangedEventArgs e)
{
var tabControl = (TabControl)sender;
//判断是子控件消息还是Tab消息
if (e.Source.GetType() == typeof(TabControl)) return;
e.Handled = true;
//继续向父级传递
if (tabControl.Parent is not UIElement tabParent) return;
var selectionEventArg =
new SelectionChangedEventArgs(e.RoutedEvent, e.RemovedItems, e.AddedItems)
{
Source = e.OriginalSource
};
tabParent.RaiseEvent(selectionEventArg);
}
// 注册 TabType 附加属性
public static readonly DependencyProperty TabTypeProperty =
DependencyProperty.RegisterAttached(
"TabType",
typeof(TabType),
typeof(TabAssist),
new PropertyMetadata(TabType.Line)); // 默认值为 Line
public static void SetTabType(DependencyObject element, TabType value)
{
element.SetValue(TabTypeProperty, value);
}
public static TabType GetTabType(DependencyObject element)
{
return (TabType)element.GetValue(TabTypeProperty);
}
// 注册 IsEditable 附加属性
public static readonly DependencyProperty IsEditableProperty =
DependencyProperty.RegisterAttached(
"IsEditable",
typeof(bool),
typeof(TabAssist),
new PropertyMetadata(false));
public static void SetIsEditable(DependencyObject element, bool value)
{
element.SetValue(IsEditableProperty, value);
}
public static bool GetIsEditable(DependencyObject element)
{
return (bool)element.GetValue(IsEditableProperty);
}
}
public enum TabType
{
Line, // 默认线条样式
Card // 卡片样式
}