功能更新
This commit is contained in:
265
Melskin/Assists/BehaviorAssist.cs
Normal file
265
Melskin/Assists/BehaviorAssist.cs
Normal file
@@ -0,0 +1,265 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media.Animation;
|
||||
using Melskin.Controls;
|
||||
using Melskin.Extensions;
|
||||
|
||||
namespace Melskin.Assists
|
||||
{
|
||||
/// <summary>
|
||||
/// 控件行为的附加属性类
|
||||
/// </summary>
|
||||
public class BehaviorAssist
|
||||
{
|
||||
#region Popup
|
||||
|
||||
/// <summary>
|
||||
/// 表示是否使 Popup 控件模拟原生平台的弹出行为。当设置为 true 时,Popup 将会在用户点击外部区域或窗口失去焦点时自动关闭,从而提供更接近于原生应用程序的用户体验。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty SimulateNativeBehaviorProperty =
|
||||
DependencyProperty.RegisterAttached(
|
||||
"SimulateNativeBehavior", typeof(bool), typeof(BehaviorAssist),
|
||||
new PropertyMetadata(false, OnSimulateNativeBehaviorChanged));
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="element"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void SetSimulateNativeBehavior(DependencyObject element, bool value) => element.SetValue(SimulateNativeBehaviorProperty, value);
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="element"></param>
|
||||
/// <returns></returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(ColorPicker))]
|
||||
[AttachedPropertyBrowsableForType(typeof(AutoComplete))]
|
||||
[AttachedPropertyBrowsableForType(typeof(Cascader))]
|
||||
[AttachedPropertyBrowsableForType(typeof(ComboBox))]
|
||||
[AttachedPropertyBrowsableForType(typeof(DatePicker))]
|
||||
[AttachedPropertyBrowsableForType(typeof(MultiComboBox))]
|
||||
[AttachedPropertyBrowsableForType(typeof(TimePicker))]
|
||||
public static bool GetSimulateNativeBehavior(DependencyObject element) => (bool)element.GetValue(SimulateNativeBehaviorProperty);
|
||||
|
||||
// 私有状态属性的定义保持不变
|
||||
private static readonly DependencyProperty PreviewMouseDownHandlerProperty =
|
||||
DependencyProperty.RegisterAttached("PreviewMouseDownHandler", typeof(MouseButtonEventHandler), typeof(BehaviorAssist));
|
||||
private static readonly DependencyProperty WindowEventHandlerProperty =
|
||||
DependencyProperty.RegisterAttached("WindowEventHandler", typeof(EventHandler), typeof(BehaviorAssist));
|
||||
private static readonly DependencyProperty PreviewMouseWheelHandlerProperty =
|
||||
DependencyProperty.RegisterAttached("PreviewMouseWheelHandler", typeof(MouseWheelEventHandler), typeof(BehaviorAssist));
|
||||
|
||||
|
||||
private static void OnSimulateNativeBehaviorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is not Popup popup) return;
|
||||
if ((bool)e.NewValue)
|
||||
{
|
||||
popup.Opened += OnPopupOpened;
|
||||
popup.Closed += OnPopupClosed;
|
||||
}
|
||||
else
|
||||
{
|
||||
popup.Opened -= OnPopupOpened;
|
||||
popup.Closed -= OnPopupClosed;
|
||||
}
|
||||
}
|
||||
|
||||
private static void OnPopupOpened(object? sender, EventArgs e)
|
||||
{
|
||||
if (sender is not Popup popup || !GetSimulateNativeBehavior(popup)) return;
|
||||
|
||||
var window = Window.GetWindow(popup);
|
||||
if (window == null) return;
|
||||
|
||||
// 【防御性编程】在添加任何处理器之前,先执行一次清理,防止因竞争条件导致的泄漏。
|
||||
CleanupHandlers(popup, window);
|
||||
|
||||
// --- 添加自动关闭逻辑 ---
|
||||
EventHandler windowEventHandler = (_, _) =>
|
||||
{
|
||||
popup.Dispatcher.BeginInvoke(new Action(() => { popup.IsOpen = false; }));
|
||||
};
|
||||
popup.SetValue(WindowEventHandlerProperty, windowEventHandler);
|
||||
window.Deactivated += windowEventHandler;
|
||||
window.LocationChanged += windowEventHandler;
|
||||
|
||||
// --- 添加鼠标点击外部逻辑 ---
|
||||
MouseButtonEventHandler mouseDownHandler = (_, _) =>
|
||||
{
|
||||
if (popup.IsMouseOver || popup.PlacementTarget is { IsMouseOver: true })
|
||||
{
|
||||
return;
|
||||
}
|
||||
popup.IsOpen = false;
|
||||
};
|
||||
popup.SetValue(PreviewMouseDownHandlerProperty, mouseDownHandler);
|
||||
window.AddHandler(UIElement.PreviewMouseDownEvent, mouseDownHandler, true);
|
||||
|
||||
// --- 添加鼠标滚轮拦截逻辑 ---
|
||||
MouseWheelEventHandler mouseWheelHandler = (_, args) =>
|
||||
{
|
||||
if (!popup.IsMouseOver)
|
||||
{
|
||||
args.Handled = true;
|
||||
}
|
||||
};
|
||||
popup.SetValue(PreviewMouseWheelHandlerProperty, mouseWheelHandler);
|
||||
window.AddHandler(UIElement.PreviewMouseWheelEvent, mouseWheelHandler, true);
|
||||
}
|
||||
|
||||
private static void OnPopupClosed(object? sender, EventArgs e)
|
||||
{
|
||||
if (sender is not Popup popup || !GetSimulateNativeBehavior(popup)) return;
|
||||
|
||||
var window = Window.GetWindow(popup);
|
||||
// 窗口可能在Popup关闭时已经不存在了
|
||||
if (window != null)
|
||||
{
|
||||
CleanupHandlers(popup, window);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 【新增】一个集中的清理方法,确保所有附加的处理器都被移除。
|
||||
/// </summary>
|
||||
private static void CleanupHandlers(Popup popup, Window window)
|
||||
{
|
||||
if (popup.GetValue(WindowEventHandlerProperty) is EventHandler windowEventHandler)
|
||||
{
|
||||
window.Deactivated -= windowEventHandler;
|
||||
window.LocationChanged -= windowEventHandler;
|
||||
popup.ClearValue(WindowEventHandlerProperty);
|
||||
}
|
||||
|
||||
if (popup.GetValue(PreviewMouseDownHandlerProperty) is MouseButtonEventHandler mouseDownHandler)
|
||||
{
|
||||
window.RemoveHandler(UIElement.PreviewMouseDownEvent, mouseDownHandler);
|
||||
popup.ClearValue(PreviewMouseDownHandlerProperty);
|
||||
}
|
||||
|
||||
if (popup.GetValue(PreviewMouseWheelHandlerProperty) is MouseWheelEventHandler mouseWheelHandler)
|
||||
{
|
||||
window.RemoveHandler(UIElement.PreviewMouseWheelEvent, mouseWheelHandler);
|
||||
popup.ClearValue(PreviewMouseWheelHandlerProperty);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ListBox
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定对象的滑动动画启用状态。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取属性值的对象。</param>
|
||||
/// <returns>如果为 true,则表示启用了滑动动画;否则为 false。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(ListBox))]
|
||||
public static bool GetEnableSlideAnimation(DependencyObject obj)
|
||||
{
|
||||
return (bool)obj.GetValue(EnableSlideAnimationProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定对象的滑动动画启用状态。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置属性值的对象。</param>
|
||||
/// <param name="value">一个布尔值,表示是否启用滑动动画。如果为 true,则启用滑动动画;否则不启用。</param>
|
||||
public static void SetEnableSlideAnimation(DependencyObject obj, bool value)
|
||||
{
|
||||
obj.SetValue(EnableSlideAnimationProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于控制 ListBox 控件是否启用滑动动画效果的依赖属性。通过设置此属性,可以为 ListBox 添加平滑的滑动过渡效果,从而增强用户体验。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty EnableSlideAnimationProperty =
|
||||
DependencyProperty.RegisterAttached("EnableSlideAnimation", typeof(bool), typeof(BehaviorAssist), new PropertyMetadata(false, OnEnableSlideAnimationChanged));
|
||||
|
||||
private static void OnEnableSlideAnimationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is not ListBox listBox) return;
|
||||
if ((bool)e.NewValue)
|
||||
{
|
||||
listBox.Loaded += ListBox_Loaded;
|
||||
listBox.SelectionChanged += ListBox_SelectionChanged;
|
||||
}
|
||||
else
|
||||
{
|
||||
listBox.Loaded -= ListBox_Loaded;
|
||||
listBox.SelectionChanged -= ListBox_SelectionChanged;
|
||||
}
|
||||
}
|
||||
|
||||
private static void ListBox_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (sender is ListBox listBox && listBox.IsVisible)
|
||||
{
|
||||
UpdateIndicator(listBox, false);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (sender is ListBox { IsLoaded: true } listBox)
|
||||
{
|
||||
UpdateIndicator(listBox, true);
|
||||
}
|
||||
}
|
||||
|
||||
private static void UpdateIndicator(ListBox? listBox, bool useAnimation)
|
||||
{
|
||||
if (listBox?.SelectedItem == null) return;
|
||||
|
||||
var indicator = listBox.Template.FindName("PART_SelectionIndicator", listBox) as FrameworkElement;
|
||||
var itemsPresenter = listBox.FindVisualChild<ItemsPresenter>();
|
||||
|
||||
if (indicator == null || itemsPresenter == null) return;
|
||||
|
||||
var selectedItemUI = listBox.ItemContainerGenerator.ContainerFromItem(listBox.SelectedItem) as FrameworkElement;
|
||||
if (selectedItemUI == null) return;
|
||||
|
||||
var position = selectedItemUI.TransformToAncestor(itemsPresenter).Transform(new Point(0, 0));
|
||||
|
||||
indicator.Width = selectedItemUI.ActualWidth;
|
||||
indicator.Height = selectedItemUI.ActualHeight;
|
||||
|
||||
var transform = indicator.RenderTransform as TranslateTransform;
|
||||
|
||||
// 如果 transform 为 null,或者它是一个被冻结的(只读)实例,
|
||||
// 那么我们就需要创建一个新的、可写的实例来替换它。
|
||||
if (transform == null || transform.IsFrozen)
|
||||
{
|
||||
transform = new TranslateTransform();
|
||||
indicator.RenderTransform = transform;
|
||||
}
|
||||
|
||||
if (useAnimation)
|
||||
{
|
||||
var animationX = new DoubleAnimation(position.X, new Duration(TimeSpan.FromMilliseconds(300)))
|
||||
{
|
||||
EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut }
|
||||
};
|
||||
var animationY = new DoubleAnimation(position.Y, new Duration(TimeSpan.FromMilliseconds(300)))
|
||||
{
|
||||
EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut }
|
||||
};
|
||||
transform.BeginAnimation(TranslateTransform.XProperty, animationX);
|
||||
transform.BeginAnimation(TranslateTransform.YProperty, animationY);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 现在 transform 保证是可写的,所以这里不会再抛出异常
|
||||
transform.X = position.X;
|
||||
transform.Y = position.Y;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
36
Melskin/Assists/BindingProxy.cs
Normal file
36
Melskin/Assists/BindingProxy.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Melskin.Assists
|
||||
{
|
||||
/// <summary>
|
||||
/// 绑定代理,用来传递绑定对象
|
||||
/// </summary>
|
||||
public class BindingProxy : Freezable
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override Freezable CreateInstanceCore() => new BindingProxy();
|
||||
|
||||
/// <summary>
|
||||
/// 绑定实例的数据
|
||||
/// </summary>
|
||||
public object Data
|
||||
{
|
||||
get => GetValue(DataProperty);
|
||||
set { SetValue(DataProperty, value); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 附加属性:Data
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty DataProperty = DependencyProperty.Register(
|
||||
nameof(Data),
|
||||
typeof(object),
|
||||
typeof(BindingProxy),
|
||||
new PropertyMetadata(null)
|
||||
);
|
||||
}
|
||||
}
|
||||
327
Melskin/Assists/ColorAssist.cs
Normal file
327
Melskin/Assists/ColorAssist.cs
Normal file
@@ -0,0 +1,327 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using Melskin.Controls;
|
||||
|
||||
namespace Melskin.Assists
|
||||
{
|
||||
/// <summary>
|
||||
/// 颜色辅助类,提供了一组附加属性,用于在WPF应用程序中为控件设置和获取各种颜色相关的属性,如标题栏背景色、鼠标悬停颜色、聚焦颜色和按下状态颜色等。
|
||||
/// </summary>
|
||||
public static class ColorAssist
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的标题栏背景画刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其标题栏背景画刷的依赖对象。</param>
|
||||
/// <returns>与指定依赖对象关联的标题栏背景画刷。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(MelWindow))]
|
||||
public static Brush GetTitleBarBackground(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(TitleBarBackgroundProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的标题栏背景画刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置标题栏背景的依赖对象。</param>
|
||||
/// <param name="value">作为标题栏背景的新画刷值。</param>
|
||||
public static void SetTitleBarBackground(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(TitleBarBackgroundProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于定义依赖属性的标识符,该属性允许设置和获取与窗口控件关联的标题栏背景画刷。此属性可以应用于任何实现了DependencyObject接口的对象上,以便自定义其标题栏背景颜色。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty TitleBarBackgroundProperty =
|
||||
DependencyProperty.RegisterAttached("TitleBarBackground", typeof(Brush), typeof(ColorAssist));
|
||||
|
||||
#region Brushes
|
||||
|
||||
/// <summary>
|
||||
/// 代表当鼠标悬停在控件上时前景色的属性。此依赖属性允许在WPF应用程序中为任意控件设置和获取鼠标悬停状态下的前景色刷。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty MouseOverForegroundProperty = DependencyProperty.RegisterAttached(
|
||||
"MouseOverForeground",
|
||||
typeof(Brush),
|
||||
typeof(ColorAssist),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// 获取与指定依赖对象关联的鼠标悬停前景色。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其鼠标悬停前景色属性的依赖对象。</param>
|
||||
/// <returns>与指定依赖对象关联的鼠标悬停前景色。</returns>
|
||||
public static Brush GetMouseOverForeground(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(MouseOverForegroundProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象在鼠标悬停时的前景色。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置其鼠标悬停前景色属性的依赖对象。</param>
|
||||
/// <param name="value">表示鼠标悬停时前景色的Brush对象。</param>
|
||||
public static void SetMouseOverForeground(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(MouseOverForegroundProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 代表当鼠标悬停在控件上时边框的颜色属性。此依赖属性允许在WPF应用程序中为任意控件设置和获取鼠标悬停状态下的边框画刷。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty MouseOverBorderBrushProperty = DependencyProperty.RegisterAttached(
|
||||
"MouseOverBorderBrush",
|
||||
typeof(Brush),
|
||||
typeof(ColorAssist),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的鼠标悬停时边框画刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其鼠标悬停时边框画刷属性的依赖对象。</param>
|
||||
/// <returns>指定依赖对象的鼠标悬停时边框画刷。</returns>
|
||||
public static Brush GetMouseOverBorderBrush(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(MouseOverBorderBrushProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的鼠标悬停边框画刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置其鼠标悬停边框画刷属性的依赖对象。</param>
|
||||
/// <param name="value">要应用到依赖对象上的鼠标悬停边框画刷。</param>
|
||||
public static void SetMouseOverBorderBrush(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(MouseOverBorderBrushProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 代表控件在鼠标悬停时的背景画刷属性。此依赖属性允许在WPF应用程序中为任意控件设置和获取鼠标悬停时的背景颜色或图案。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty MouseOverBackgroundProperty = DependencyProperty.RegisterAttached(
|
||||
"MouseOverBackground",
|
||||
typeof(Brush),
|
||||
typeof(ColorAssist),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的鼠标悬停时的背景画刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其鼠标悬停背景画刷属性的依赖对象。</param>
|
||||
/// <returns>指定依赖对象的鼠标悬停背景画刷。</returns>
|
||||
public static Brush GetMouseOverBackground(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(MouseOverBackgroundProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的鼠标悬停背景画刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置其鼠标悬停背景画刷属性的依赖对象。</param>
|
||||
/// <param name="value">新的鼠标悬停背景画刷值。</param>
|
||||
public static void SetMouseOverBackground(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(MouseOverBackgroundProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 代表控件在获得焦点时的前景色属性。此依赖属性允许为WPF应用程序中的控件设置和获取焦点状态下的前景色,支持继承,并影响渲染。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty FocusedForegroundProperty = DependencyProperty.RegisterAttached(
|
||||
"FocusedForeground",
|
||||
typeof(Brush),
|
||||
typeof(ColorAssist),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// 获取与指定依赖对象关联的聚焦前景色。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其聚焦前景色属性的依赖对象。</param>
|
||||
/// <returns>与指定依赖对象关联的聚焦前景色。</returns>
|
||||
public static Brush GetFocusedForeground(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(FocusedForegroundProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的聚焦前景色。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置其聚焦前景色属性的依赖对象。</param>
|
||||
/// <param name="value">新聚焦前景色值。</param>
|
||||
public static void SetFocusedForeground(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(FocusedForegroundProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 代表控件在获得焦点时边框的画刷属性。此依赖属性允许在WPF应用程序中为任意控件设置和获取焦点状态下的边框画刷。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty FocusedBorderBrushProperty = DependencyProperty.RegisterAttached(
|
||||
"FocusedBorderBrush",
|
||||
typeof(Brush),
|
||||
typeof(ColorAssist),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// 获取与指定依赖对象关联的聚焦边框画刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其聚焦边框画刷属性的依赖对象。</param>
|
||||
/// <returns>与指定依赖对象关联的聚焦边框画刷。</returns>
|
||||
public static Brush GetFocusedBorderBrush(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(FocusedBorderBrushProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的焦点边框画刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置其焦点边框画刷属性的依赖对象。</param>
|
||||
/// <param name="value">要设置的焦点边框画刷值。</param>
|
||||
public static void SetFocusedBorderBrush(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(FocusedBorderBrushProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 代表控件获得焦点时的背景颜色属性。此依赖属性允许在WPF应用程序中为任意控件设置和获取当其处于焦点状态下的背景颜色,支持继承。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty FocusedBackgroundProperty = DependencyProperty.RegisterAttached(
|
||||
"FocusedBackground",
|
||||
typeof(Brush),
|
||||
typeof(ColorAssist),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的聚焦背景画刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其聚焦背景画刷属性的依赖对象。</param>
|
||||
/// <returns>指定依赖对象的聚焦背景画刷。</returns>
|
||||
public static Brush GetFocusedBackground(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(FocusedBackgroundProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的焦点背景。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置其焦点背景属性的依赖对象。</param>
|
||||
/// <param name="value">要设置的焦点背景画刷。</param>
|
||||
public static void SetFocusedBackground(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(FocusedBackgroundProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 代表控件被按下时的前景色属性。此依赖属性允许在WPF应用程序中为任意控件设置和获取按下的前景色,并且支持继承。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty PressedForegroundProperty = DependencyProperty.RegisterAttached(
|
||||
"PressedForeground",
|
||||
typeof(Brush),
|
||||
typeof(ColorAssist),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的按下状态前景色。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其按下状态前景色属性的依赖对象。</param>
|
||||
/// <returns>指定依赖对象的按下状态前景色。</returns>
|
||||
public static Brush GetPressedForeground(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(PressedForegroundProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的按下状态前景色。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置其按下状态前景色属性的依赖对象。</param>
|
||||
/// <param name="value">新的按下状态前景色。</param>
|
||||
public static void SetPressedForeground(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(PressedForegroundProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 代表控件在按下状态时的边框画刷属性。此依赖属性允许在WPF应用程序中为任意控件设置和获取按下状态下的边框颜色或纹理。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty PressedBorderBrushProperty = DependencyProperty.RegisterAttached(
|
||||
"PressedBorderBrush",
|
||||
typeof(Brush),
|
||||
typeof(ColorAssist),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的按下状态边框画刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其按下状态边框画刷属性的依赖对象。</param>
|
||||
/// <returns>指定依赖对象的按下状态边框画刷。</returns>
|
||||
public static Brush GetPressedBorderBrush(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(PressedBorderBrushProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的按下状态边框画刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置其按下状态边框画刷属性的依赖对象。</param>
|
||||
/// <param name="value">要设置的按下状态边框画刷。</param>
|
||||
public static void SetPressedBorderBrush(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(PressedBorderBrushProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 代表控件在按下状态时的背景色属性。此依赖属性允许为WPF应用程序中的任意控件设置和获取按下状态时的背景颜色,支持继承并影响渲染。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty PressedBackgroundProperty = DependencyProperty.RegisterAttached(
|
||||
"PressedBackground",
|
||||
typeof(Brush),
|
||||
typeof(ColorAssist),
|
||||
new FrameworkPropertyMetadata(
|
||||
Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的按下背景画刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其按下背景画刷属性的依赖对象。</param>
|
||||
/// <returns>指定依赖对象的按下背景画刷。</returns>
|
||||
public static Brush GetPressedBackground(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(PressedBackgroundProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的按下状态背景。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置其按下状态背景的依赖对象。</param>
|
||||
/// <param name="value">按下状态时使用的背景画刷。</param>
|
||||
public static void SetPressedBackground(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(PressedBackgroundProperty, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
509
Melskin/Assists/ControlAssist.cs
Normal file
509
Melskin/Assists/ControlAssist.cs
Normal file
@@ -0,0 +1,509 @@
|
||||
|
||||
using System.Windows.Controls.Primitives;
|
||||
using Melskin.Controls;
|
||||
|
||||
namespace Melskin.Assists;
|
||||
|
||||
using System.ComponentModel;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
|
||||
/// <summary>
|
||||
/// 提供各种控件的附加属性帮助类。
|
||||
/// </summary>
|
||||
public static class ControlAssist
|
||||
{
|
||||
/// <summary>
|
||||
/// 按钮变体附加属性的获取方法。
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static Variant GetVariant(DependencyObject obj)
|
||||
{
|
||||
return (Variant)obj.GetValue(VariantProperty);
|
||||
}
|
||||
/// <summary>
|
||||
/// 按钮变体附加属性的设置方法。
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void SetMyProperty(DependencyObject obj, Variant value)
|
||||
{
|
||||
obj.SetValue(VariantProperty, value);
|
||||
}
|
||||
/// <summary>
|
||||
/// 按钮变体附加属性
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty VariantProperty =
|
||||
DependencyProperty.RegisterAttached("Variant", typeof(Variant), typeof(ControlAssist), new PropertyMetadata(Variant.Default));
|
||||
|
||||
/// <summary>
|
||||
/// 定义一个附加属性 IsFlowLayout (或者叫 UseWrapPanel)
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsFlowLayoutProperty =
|
||||
DependencyProperty.RegisterAttached(
|
||||
"IsFlowLayout",
|
||||
typeof(bool),
|
||||
typeof(ControlAssist),
|
||||
new PropertyMetadata(false));
|
||||
/// <summary>
|
||||
/// 流式布局
|
||||
/// </summary>
|
||||
/// <param name="element"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void SetIsFlowLayout(DependencyObject element, bool value)
|
||||
{
|
||||
element.SetValue(IsFlowLayoutProperty, value);
|
||||
}
|
||||
/// <summary>
|
||||
/// 流式布局附加属性的获取方法。
|
||||
/// </summary>
|
||||
/// <param name="element"></param>
|
||||
/// <returns></returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(ListBox))]
|
||||
public static bool GetIsFlowLayout(DependencyObject element)
|
||||
{
|
||||
return (bool)element.GetValue(IsFlowLayoutProperty);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取指定对象的外观类型。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其外观类型的依赖对象。</param>
|
||||
/// <returns>返回指定对象的AppearanceType枚举值,表示其外观类型。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(Button))]
|
||||
public static AppearanceType GetAppearanceType(DependencyObject obj)
|
||||
{
|
||||
return (AppearanceType)obj.GetValue(AppearanceTypeProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定对象的外观类型。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置其外观类型的依赖对象。</param>
|
||||
/// <param name="value">AppearanceType枚举值,表示要设置的外观类型。</param>
|
||||
public static void SetAppearanceType(DependencyObject obj, AppearanceType value)
|
||||
{
|
||||
obj.SetValue(AppearanceTypeProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 代表按钮控件的外观类型依赖属性。通过此属性,可以设置或获取按钮的视觉样式。
|
||||
/// 支持的样式包括Primary、Info、Success、Warning和Error,分别对应不同的视觉效果。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty AppearanceTypeProperty =
|
||||
DependencyProperty.RegisterAttached("AppearanceType", typeof(AppearanceType), typeof(ControlAssist), new PropertyMetadata(AppearanceType.None));
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定对象的图标。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其图标的依赖对象。</param>
|
||||
/// <returns>返回指定对象的IconElement,表示其图标。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(Button))]
|
||||
[AttachedPropertyBrowsableForType(typeof(Menu))]
|
||||
public static IconElement GetIcon(DependencyObject obj)
|
||||
{
|
||||
return (IconElement)obj.GetValue(IconProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 为指定对象设置图标。
|
||||
/// </summary>
|
||||
/// <param name="obj">要为其设置图标的依赖对象。</param>
|
||||
/// <param name="value">要设置的IconElement值。</param>
|
||||
public static void SetIcon(DependencyObject obj, IconElement value)
|
||||
{
|
||||
obj.SetValue(IconProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 代表按钮控件的图标依赖属性。通过此属性,可以设置或获取按钮上显示的图标。
|
||||
/// 支持使用IconElement来指定具体的图标样式。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IconProperty =
|
||||
DependencyProperty.RegisterAttached("Icon", typeof(IconElement), typeof(ControlAssist), new PropertyMetadata(null));
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定对象的图标放置位置。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其图标放置位置的依赖对象。</param>
|
||||
/// <returns>返回指定对象的Dock枚举值,表示图标的放置位置。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(Button))]
|
||||
public static Dock GetIconPlacement(DependencyObject obj)
|
||||
{
|
||||
return (Dock)obj.GetValue(IconPlacementProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定对象的图标放置位置。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置其图标放置位置的依赖对象。</param>
|
||||
/// <param name="value">图标放置位置的值,使用Dock枚举表示。</param>
|
||||
public static void SetIconPlacement(DependencyObject obj, Dock value)
|
||||
{
|
||||
obj.SetValue(IconPlacementProperty, value);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 代表按钮控件的图标位置依赖属性。通过此属性,可以设置或获取按钮上图标的放置位置。
|
||||
/// 支持使用Dock枚举中的值来指定图标相对于按钮文本的位置,例如左对齐、右对齐等。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IconPlacementProperty =
|
||||
DependencyProperty.RegisterAttached("IconPlacement", typeof(Dock), typeof(ControlAssist), new PropertyMetadata(Dock.Left));
|
||||
|
||||
/// <summary>
|
||||
/// 代表按钮控件是否正在运行的依赖属性。通过此属性,可以设置或获取按钮的运行状态。
|
||||
/// 此属性通常用于指示按钮关联的操作是否正在进行中,例如数据加载或处理过程。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsRunningProperty =
|
||||
DependencyProperty.RegisterAttached("IsRunning", typeof(bool), typeof(ControlAssist), new PropertyMetadata(false));
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定对象的IsRunning状态。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其IsRunning状态的依赖对象。</param>
|
||||
/// <returns>返回一个布尔值,表示指定对象是否正在运行。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(Button))]
|
||||
public static bool GetIsRunning(DependencyObject obj) => (bool)obj.GetValue(IsRunningProperty);
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定对象的运行状态。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置其运行状态的依赖对象。</param>
|
||||
/// <param name="value">布尔值,表示对象是否处于运行状态。</param>
|
||||
public static void SetIsRunning(DependencyObject obj, bool value) => obj.SetValue(IsRunningProperty, value);
|
||||
|
||||
/// <summary>
|
||||
/// 附加属性,用于为UI元素指定一个字符串形式的标识符(锚点名称)。此属性允许开发者将特定的标识字符串与UI元素关联起来,
|
||||
/// 从而在WPF应用程序中实现基于内容滚动位置自动更新当前高亮显示的锚点列表项等功能。通过设置该属性,可以方便地进行用户界面中的快速导航。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty AnchorHeaderTextProperty =
|
||||
DependencyProperty.RegisterAttached(
|
||||
"AnchorHeaderText",
|
||||
typeof(string),
|
||||
typeof(ControlAssist),
|
||||
new PropertyMetadata(null));
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的Header属性值。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取Header属性值的依赖对象。</param>
|
||||
/// <returns>返回指定依赖对象的Header属性值,如果未设置则返回null。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(Anchor))]
|
||||
public static string GetAnchorHeaderText(DependencyObject obj)
|
||||
{
|
||||
return (string)obj.GetValue(AnchorHeaderTextProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 为指定依赖对象设置Header属性值。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置Header属性的依赖对象。</param>
|
||||
/// <param name="value">要设置的Header属性值,类型为字符串。</param>
|
||||
public static void SetAnchorHeaderText(DependencyObject obj, string value)
|
||||
{
|
||||
obj.SetValue(AnchorHeaderTextProperty, value);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取指定对象的Orientation属性值。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取Orientation属性的对象。</param>
|
||||
/// <returns>返回对象的Orientation属性值,可以是Horizontal或Vertical。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(ListBox))]
|
||||
public static Orientation GetOrientation(DependencyObject obj)
|
||||
{
|
||||
return (Orientation)obj.GetValue(OrientationProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定对象的Orientation属性。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置Orientation属性的对象。</param>
|
||||
/// <param name="value">要设置的Orientation值,可以是Horizontal或Vertical。</param>
|
||||
public static void SetOrientation(DependencyObject obj, Orientation value)
|
||||
{
|
||||
obj.SetValue(OrientationProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 表示选择器控件的方向属性,可以用来设置或获取指定依赖对象的Orientation属性值。该属性支持两种方向:Horizontal(水平)和Vertical(垂直),默认值为Vertical。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty OrientationProperty =
|
||||
DependencyProperty.RegisterAttached("Orientation", typeof(Orientation), typeof(ControlAssist), new PropertyMetadata(Orientation.Vertical));
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的SynchronizedScroll属性值。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取SynchronizedScroll属性值的依赖对象。</param>
|
||||
/// <returns>返回与指定依赖对象关联的ScrollViewer。如果未设置,则返回null。</returns>
|
||||
public static ScrollViewer GetSynchronizedScroll(DependencyObject obj)
|
||||
{
|
||||
return (ScrollViewer)obj.GetValue(SynchronizedScrollProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的SynchronizedScroll属性值。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置SynchronizedScroll属性的依赖对象。</param>
|
||||
/// <param name="value">要设置的ScrollViewer值,用于同步滚动位置。</param>
|
||||
public static void SetSynchronizedScroll(DependencyObject obj, ScrollViewer value)
|
||||
{
|
||||
obj.SetValue(SynchronizedScrollProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SynchronizedScrollProperty 是一个依赖属性,用于在多个 ScrollViewer 控件之间同步滚动位置。通过此属性,可以设置或获取与特定依赖对象关联的 ScrollViewer 实例,从而实现当一个 ScrollViewer 的滚动位置发生变化时,其他关联的 ScrollViewer 也能自动调整其滚动位置以保持一致。
|
||||
/// 主要用于在 WPF 应用程序中实现多 ScrollViewer 之间的滚动同步功能,特别适用于需要同时滚动显示不同内容但保持视觉一致性的场景。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty SynchronizedScrollProperty =
|
||||
DependencyProperty.RegisterAttached("SynchronizedScroll",
|
||||
typeof(ScrollViewer), typeof(ControlAssist),
|
||||
new PropertyMetadata(null, OnSynchronizedScrollChanged));
|
||||
|
||||
private static void OnSynchronizedScrollChanged(DependencyObject d,
|
||||
DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is not ScrollViewer scroll)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//从无到有
|
||||
if (e.OldValue == null && e.NewValue != null)
|
||||
{
|
||||
scroll.ScrollChanged += SynchronizeScrollViewer;
|
||||
}
|
||||
}
|
||||
|
||||
private static void SynchronizeScrollViewer(object sender, ScrollChangedEventArgs e)
|
||||
{
|
||||
var senderScroll = sender as ScrollViewer;
|
||||
if (senderScroll == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//获取要被驱动的ScrollViewer
|
||||
var targetScroll = GetSynchronizedScroll(senderScroll);
|
||||
targetScroll.ScrollToHorizontalOffset(senderScroll.HorizontalOffset);
|
||||
}
|
||||
|
||||
#region Toggle
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的开关处于关闭状态时显示的文本。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取关闭状态文本的依赖对象。</param>
|
||||
/// <returns>返回一个字符串,表示开关处于关闭状态时显示的文本。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(ToggleButton))]
|
||||
|
||||
public static string GetOffText(DependencyObject obj)
|
||||
{
|
||||
return (string)obj.GetValue(OffTextProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置开关控件在关闭状态时显示的文本。
|
||||
/// </summary>
|
||||
/// <param name="obj">依赖对象,通常为开关控件实例。</param>
|
||||
/// <param name="value">要设置的文本值。</param>
|
||||
public static void SetOffText(DependencyObject obj, string value)
|
||||
{
|
||||
obj.SetValue(OffTextProperty, value);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 用于获取或设置开关处于关闭状态时显示的文本。
|
||||
/// 默认值为"关"。此依赖属性支持动画、样式和数据绑定等功能。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty OffTextProperty =
|
||||
DependencyProperty.RegisterAttached("OffText", typeof(string), typeof(ControlAssist), new PropertyMetadata("关"));
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的开关处于开启状态时显示的文本。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取开启状态文本的依赖对象。</param>
|
||||
/// <returns>返回一个字符串,表示开关处于开启状态时显示的文本。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(ToggleButton))]
|
||||
public static string GetOnText(DependencyObject obj)
|
||||
{
|
||||
return (string)obj.GetValue(OnTextProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的开关处于开启状态时显示的文本。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置开启状态文本的依赖对象。</param>
|
||||
/// <param name="value">要设置的文本值。</param>
|
||||
public static void SetOnText(DependencyObject obj, string value)
|
||||
{
|
||||
obj.SetValue(OnTextProperty, value);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 用于获取或设置开关处于开启状态时显示的文本。
|
||||
/// 默认值为"开"。此依赖属性支持动画、样式和数据绑定等功能。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty OnTextProperty =
|
||||
DependencyProperty.RegisterAttached("OnText", typeof(string), typeof(ControlAssist), new PropertyMetadata("开"));
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的开关是否显示文本的状态。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取显示文本状态的依赖对象。</param>
|
||||
/// <returns>返回一个布尔值,表示开关是否显示文本。如果为true,则显示文本;如果为false,则不显示文本。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(ToggleButton))]
|
||||
public static bool GetShowText(DependencyObject obj)
|
||||
{
|
||||
return (bool)obj.GetValue(ShowTextProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的开关控件是否显示文本。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置显示文本状态的依赖对象。</param>
|
||||
/// <param name="value">布尔值,表示是否显示文本。如果为true,则显示文本;如果为false,则不显示文本。</param>
|
||||
public static void SetShowText(DependencyObject obj, bool value)
|
||||
{
|
||||
obj.SetValue(ShowTextProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于获取或设置是否显示开关状态文本。
|
||||
/// 默认值为false。此依赖属性支持动画、样式和数据绑定等功能。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShowTextProperty =
|
||||
DependencyProperty.RegisterAttached("ShowText", typeof(bool), typeof(ControlAssist), new PropertyMetadata(false));
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的开关处于关闭状态时的内容。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取关闭状态内容的依赖对象。</param>
|
||||
/// <returns>返回一个对象,表示开关处于关闭状态时显示的内容。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(ToggleButton))]
|
||||
public static object GetOffContent(DependencyObject obj)
|
||||
{
|
||||
return (object)obj.GetValue(OffContentProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的开关处于关闭状态时显示的内容。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置关闭状态内容的依赖对象。</param>
|
||||
/// <param name="value">要设置为关闭状态时显示的内容。</param>
|
||||
public static void SetOffContent(DependencyObject obj, object value)
|
||||
{
|
||||
obj.SetValue(OffContentProperty, value);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 用于获取或设置开关处于关闭状态时显示的内容。
|
||||
/// 默认值为null。此依赖属性支持动画、样式和数据绑定等功能。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty OffContentProperty =
|
||||
DependencyProperty.RegisterAttached("OffContent", typeof(object), typeof(ControlAssist), new PropertyMetadata(null));
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的开关处于开启状态时的内容。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取开启状态内容的依赖对象。</param>
|
||||
/// <returns>返回一个对象,表示开关处于开启状态时的内容。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(ToggleButton))]
|
||||
public static object GetOnContent(DependencyObject obj)
|
||||
{
|
||||
return (object)obj.GetValue(OnContentProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的开关处于开启状态时显示的内容。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置开启状态内容的依赖对象。</param>
|
||||
/// <param name="value">要设置为开启状态时显示的内容,可以是任何类型的对象,如字符串或UI元素。</param>
|
||||
public static void SetOnContent(DependencyObject obj, object value)
|
||||
{
|
||||
obj.SetValue(OnContentProperty, value);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 用于获取或设置开关处于开启状态时显示的内容。
|
||||
/// 默认值为null。此依赖属性支持动画、样式和数据绑定等功能。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty OnContentProperty =
|
||||
DependencyProperty.RegisterAttached("OnContent", typeof(object), typeof(ControlAssist), new PropertyMetadata(null));
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Border
|
||||
|
||||
/// <summary>
|
||||
/// 获取与指定依赖对象关联的几何图形。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其几何图形属性的依赖对象。</param>
|
||||
/// <returns>与指定依赖对象关联的几何图形。</returns>
|
||||
public static Geometry GetGeometry(DependencyObject obj)
|
||||
{
|
||||
return (Geometry)obj.GetValue(GeometryProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置与指定依赖对象关联的几何图形。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置其几何图形属性的依赖对象。</param>
|
||||
/// <param name="value">要与此依赖对象关联的新几何图形值。</param>
|
||||
public static void SetGeometry(DependencyObject obj, Geometry value)
|
||||
{
|
||||
obj.SetValue(GeometryProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 代表附加到控件的几何图形属性。此依赖属性允许在WPF应用程序中为任意控件设置和获取几何图形。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty GeometryProperty =
|
||||
DependencyProperty.RegisterAttached("Geometry",
|
||||
typeof(Geometry), typeof(ControlAssist));
|
||||
|
||||
/// <summary>
|
||||
/// 代表附加到控件的圆角半径属性。此依赖属性允许在WPF应用程序中为任意控件设置和获取圆角半径,影响控件的测量和渲染。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.RegisterAttached(
|
||||
"CornerRadius",
|
||||
typeof(CornerRadius),
|
||||
typeof(ControlAssist),
|
||||
new FrameworkPropertyMetadata(
|
||||
new CornerRadius(4),
|
||||
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender
|
||||
));
|
||||
|
||||
/// <summary>
|
||||
/// 获取与指定依赖对象关联的圆角半径。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取其圆角半径属性的依赖对象。</param>
|
||||
/// <returns>与指定依赖对象关联的圆角半径。</returns>
|
||||
[Category("NeuAssists")]
|
||||
public static CornerRadius GetCornerRadius(DependencyObject obj)
|
||||
{
|
||||
return (CornerRadius)obj.GetValue(CornerRadiusProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置与指定依赖对象关联的圆角半径。
|
||||
/// </summary>
|
||||
public static void SetCornerRadius(DependencyObject obj, CornerRadius value)
|
||||
{
|
||||
obj.SetValue(CornerRadiusProperty, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
}
|
||||
463
Melskin/Assists/DataGridAssist.cs
Normal file
463
Melskin/Assists/DataGridAssist.cs
Normal file
@@ -0,0 +1,463 @@
|
||||
namespace Melskin.Assists;
|
||||
|
||||
//https://stackoverflow.com/questions/2630292/why-cant-i-style-a-datagridtextcolumn
|
||||
/// <summary>
|
||||
/// DataGrid样式注入
|
||||
/// </summary>
|
||||
public class DataGridAssist
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象是否应用默认样式。
|
||||
/// </summary>
|
||||
/// <param name="obj">要检查的依赖对象。</param>
|
||||
/// <returns>如果依赖对象设置了应用默认样式,则返回true;否则返回false。</returns>
|
||||
public static bool GetApplyDefaultStyle(DependencyObject obj)
|
||||
{
|
||||
return (bool)obj.GetValue(ApplyDefaultStyleProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象是否应用默认样式。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置的依赖对象。</param>
|
||||
/// <param name="value">布尔值,表示是否应用默认样式。</param>
|
||||
public static void SetApplyDefaultStyle(DependencyObject obj, bool value)
|
||||
{
|
||||
obj.SetValue(ApplyDefaultStyleProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于指定是否对DataGrid应用默认样式。当设置为true时,会根据DataGridAssist类中的定义自动应用一系列默认样式到DataGrid及其列上;若设为false,则不会应用这些样式。
|
||||
/// 此属性主要帮助开发者快速设定DataGrid的外观而无需手动配置每个列的样式。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ApplyDefaultStyleProperty =
|
||||
DependencyProperty.RegisterAttached("ApplyDefaultStyle",
|
||||
typeof(bool), typeof(DataGridAssist),
|
||||
new PropertyMetadata(false, OnApplyDefaultStyleChanged));
|
||||
|
||||
private static void OnApplyDefaultStyleChanged(DependencyObject d,
|
||||
DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var grid = (DataGrid)d;
|
||||
if ((bool)e.NewValue)
|
||||
{
|
||||
if (grid.AutoGenerateColumns)
|
||||
{
|
||||
grid.AutoGeneratedColumns += Grid_AutoGeneratedColumns;
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateTextColumnStyles(grid);
|
||||
UpdateEditingTextColumnStyles(grid);
|
||||
UpdateCheckBoxColumnStyles(grid);
|
||||
UpdateEditingCheckBoxColumnStyles(grid);
|
||||
UpdateComboBoxColumnStyles(grid);
|
||||
UpdateEditingComboBoxColumnStyles(grid);
|
||||
}
|
||||
else
|
||||
{
|
||||
grid.AutoGeneratedColumns -= Grid_AutoGeneratedColumns;
|
||||
}
|
||||
}
|
||||
|
||||
private static void Grid_AutoGeneratedColumns(object? sender, EventArgs e)
|
||||
{
|
||||
if (sender == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var grid = (DataGrid)sender;
|
||||
UpdateTextColumnStyles(grid);
|
||||
UpdateEditingTextColumnStyles(grid);
|
||||
UpdateCheckBoxColumnStyles(grid);
|
||||
UpdateEditingCheckBoxColumnStyles(grid);
|
||||
UpdateComboBoxColumnStyles(grid);
|
||||
UpdateEditingComboBoxColumnStyles(grid);
|
||||
}
|
||||
|
||||
|
||||
#region 文本列注入
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的文本列样式。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取样式的依赖对象。</param>
|
||||
/// <returns>返回指定依赖对象的文本列样式;如果没有设置,则返回默认值。</returns>
|
||||
public static Style GetTextColumnStyle(DependencyObject obj)
|
||||
{
|
||||
return (Style)obj.GetValue(TextColumnStyleProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的文本列样式。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置样式的依赖对象。</param>
|
||||
/// <param name="value">要应用到依赖对象上的样式。</param>
|
||||
public static void SetTextColumnStyle(DependencyObject obj, Style value)
|
||||
{
|
||||
obj.SetValue(TextColumnStyleProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于设置或获取DataGrid中的文本列样式。此属性允许开发者自定义DataGrid内文本列的显示样式,比如字体、颜色等。
|
||||
/// 通过设置这个属性,可以统一控制所有文本列的外观,简化了单独为每个列设置样式的复杂过程。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty TextColumnStyleProperty =
|
||||
DependencyProperty.RegisterAttached("TextColumnStyle",
|
||||
typeof(Style), typeof(DataGridAssist),
|
||||
new PropertyMetadata(null, OnTextColumnStyleChanged));
|
||||
|
||||
private static void OnTextColumnStyleChanged(DependencyObject d,
|
||||
DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var grid = (DataGrid)d;
|
||||
if (e.OldValue == null && e.NewValue != null)
|
||||
{
|
||||
UpdateTextColumnStyles(grid);
|
||||
}
|
||||
}
|
||||
|
||||
private static void UpdateTextColumnStyles(DataGrid grid)
|
||||
{
|
||||
var textColumnStyle = GetTextColumnStyle(grid);
|
||||
foreach (var column in grid.Columns.OfType<DataGridTextColumn>())
|
||||
{
|
||||
var elementStyle = new Style
|
||||
{
|
||||
BasedOn = column.ElementStyle,
|
||||
TargetType = textColumnStyle.TargetType
|
||||
};
|
||||
|
||||
foreach (var setter in textColumnStyle.Setters.OfType<Setter>())
|
||||
{
|
||||
elementStyle.Setters.Add(setter);
|
||||
}
|
||||
|
||||
column.ElementStyle = elementStyle;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 编辑列注入
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的编辑文本列样式。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取样式的依赖对象。</param>
|
||||
/// <returns>返回指定依赖对象的编辑文本列样式;如果未设置,则返回默认值。</returns>
|
||||
public static Style GetEditingTextColumnStyle(DependencyObject obj)
|
||||
{
|
||||
return (Style)obj.GetValue(EditingTextColumnStyleProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的编辑文本列样式。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置样式的依赖对象。</param>
|
||||
/// <param name="value">要应用到依赖对象的新样式。</param>
|
||||
public static void SetEditingTextColumnStyle(DependencyObject obj, Style value)
|
||||
{
|
||||
obj.SetValue(EditingTextColumnStyleProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于设置或获取DataGrid文本列在编辑状态下的样式。通过此属性,开发者可以自定义当DataGrid中的某列处于编辑模式时的外观风格。
|
||||
/// 这有助于提高用户界面的一致性和美观性,同时提供更加灵活的UI定制能力。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty EditingTextColumnStyleProperty =
|
||||
DependencyProperty.RegisterAttached("EditingTextColumnStyle",
|
||||
typeof(Style), typeof(DataGridAssist),
|
||||
new PropertyMetadata(null, OnEditingTextColumnStyleChanged));
|
||||
|
||||
private static void OnEditingTextColumnStyleChanged(DependencyObject d,
|
||||
DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var grid = (DataGrid)d;
|
||||
if (e.OldValue == null && e.NewValue != null)
|
||||
{
|
||||
UpdateEditingTextColumnStyles(grid);
|
||||
}
|
||||
}
|
||||
|
||||
private static void UpdateEditingTextColumnStyles(DataGrid grid)
|
||||
{
|
||||
var editingTextColumnStyle = GetEditingTextColumnStyle(grid);
|
||||
|
||||
foreach (var column in grid.Columns.OfType<DataGridTextColumn>())
|
||||
{
|
||||
var editingElementStyle = new Style
|
||||
{
|
||||
BasedOn = column.EditingElementStyle,
|
||||
TargetType = editingTextColumnStyle.TargetType
|
||||
};
|
||||
|
||||
foreach (var setter in editingTextColumnStyle.Setters.OfType<Setter>())
|
||||
{
|
||||
editingElementStyle.Setters.Add(setter);
|
||||
}
|
||||
|
||||
column.EditingElementStyle = editingElementStyle;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 勾选框注入
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static Style GetCheckBoxColumnStyle(DependencyObject obj)
|
||||
{
|
||||
return (Style)obj.GetValue(CheckBoxColumnStyleProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的复选框列样式。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置样式的依赖对象。</param>
|
||||
/// <param name="value">要应用的样式。</param>
|
||||
public static void SetCheckBoxColumnStyle(DependencyObject obj, Style value)
|
||||
{
|
||||
obj.SetValue(CheckBoxColumnStyleProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于获取或设置复选框列的样式。此属性允许开发者为DataGrid中的复选框列自定义样式,以满足特定的设计需求。
|
||||
/// 通过设置该属性,可以改变复选框列在DataGrid中的外观表现,例如修改其大小、颜色或其他视觉属性。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty CheckBoxColumnStyleProperty =
|
||||
DependencyProperty.RegisterAttached("CheckBoxColumnStyle",
|
||||
typeof(Style), typeof(DataGridAssist),
|
||||
new PropertyMetadata(null, OnCheckBoxColumnStyleChanged));
|
||||
|
||||
private static void OnCheckBoxColumnStyleChanged(DependencyObject d,
|
||||
DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var grid = (DataGrid)d;
|
||||
if (e.OldValue == null && e.NewValue != null)
|
||||
{
|
||||
UpdateCheckBoxColumnStyles(grid);
|
||||
}
|
||||
}
|
||||
|
||||
private static void UpdateCheckBoxColumnStyles(DataGrid grid)
|
||||
{
|
||||
var checkBoxColumnStyle = GetCheckBoxColumnStyle(grid);
|
||||
|
||||
foreach (var column in grid.Columns.OfType<DataGridCheckBoxColumn>())
|
||||
{
|
||||
var checkBoxElementStyle = new Style
|
||||
{
|
||||
BasedOn = column.ElementStyle,
|
||||
TargetType = checkBoxColumnStyle.TargetType
|
||||
};
|
||||
|
||||
foreach (var setter in checkBoxColumnStyle.Setters.OfType<Setter>())
|
||||
{
|
||||
checkBoxElementStyle.Setters.Add(setter);
|
||||
}
|
||||
|
||||
column.ElementStyle = checkBoxElementStyle;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 勾选框编辑
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的编辑复选框列样式。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取样式的依赖对象。</param>
|
||||
/// <returns>返回指定依赖对象的编辑复选框列样式;如果没有设置,则返回默认值。</returns>
|
||||
public static Style GetEditingCheckBoxColumnStyle(DependencyObject obj)
|
||||
{
|
||||
return (Style)obj.GetValue(EditingCheckBoxColumnStyleProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的编辑复选框列样式。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置样式的依赖对象。</param>
|
||||
/// <param name="value">要应用的新样式。</param>
|
||||
public static void SetEditingCheckBoxColumnStyle(DependencyObject obj, Style value)
|
||||
{
|
||||
obj.SetValue(EditingCheckBoxColumnStyleProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于设置或获取DataGrid中复选框列在编辑模式下的样式。此属性允许开发者自定义当单元格进入编辑状态时,复选框控件的外观和行为。
|
||||
/// 通过为该属性提供一个Style对象,可以控制诸如背景色、边框、字体等视觉元素,以及事件处理程序等交互逻辑,从而实现更符合应用需求的界面表现。
|
||||
/// </summary>
|
||||
// Using a DependencyProperty as the backing store for EditingCheckBoxColumnStyle.
|
||||
public static readonly DependencyProperty EditingCheckBoxColumnStyleProperty =
|
||||
DependencyProperty.RegisterAttached("EditingCheckBoxColumnStyle",
|
||||
typeof(Style), typeof(DataGridAssist),
|
||||
new PropertyMetadata(null, OnEditingCheckBoxColumnStyleChanged));
|
||||
|
||||
private static void OnEditingCheckBoxColumnStyleChanged(DependencyObject d,
|
||||
DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var grid = (DataGrid)d;
|
||||
if (e.OldValue == null && e.NewValue != null)
|
||||
{
|
||||
UpdateEditingCheckBoxColumnStyles(grid);
|
||||
}
|
||||
}
|
||||
|
||||
private static void UpdateEditingCheckBoxColumnStyles(DataGrid grid)
|
||||
{
|
||||
var editingCheckBoxColumnStyle = GetEditingCheckBoxColumnStyle(grid);
|
||||
|
||||
foreach (var column in grid.Columns.OfType<DataGridCheckBoxColumn>())
|
||||
{
|
||||
var checkBoxElementStyle = new Style
|
||||
{
|
||||
BasedOn = column.EditingElementStyle,
|
||||
TargetType = editingCheckBoxColumnStyle.TargetType
|
||||
};
|
||||
|
||||
foreach (var setter in editingCheckBoxColumnStyle.Setters.OfType<Setter>())
|
||||
{
|
||||
checkBoxElementStyle.Setters.Add(setter);
|
||||
}
|
||||
|
||||
column.EditingElementStyle = checkBoxElementStyle;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ComboBox列注入
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的ComboBox列样式。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取样式的依赖对象。</param>
|
||||
/// <returns>返回指定依赖对象的ComboBox列样式,如果未设置则返回默认值。</returns>
|
||||
public static Style GetComboBoxColumnStyle(DependencyObject obj)
|
||||
{
|
||||
return (Style)obj.GetValue(ComboBoxColumnStyleProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的组合框列样式。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置样式的依赖对象。</param>
|
||||
/// <param name="value">要应用到依赖对象的新样式。</param>
|
||||
public static void SetComboBoxColumnStyle(DependencyObject obj, Style value)
|
||||
{
|
||||
obj.SetValue(ComboBoxColumnStyleProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于设置或获取DataGrid中ComboBox列的样式。此依赖属性允许开发者为DataGrid中的特定列指定一个自定义的ComboBox样式,从而实现更丰富的UI定制。
|
||||
/// 通过该属性,可以方便地控制ComboBox列在显示和编辑状态下的外观表现,例如字体、颜色、边框等样式属性。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ComboBoxColumnStyleProperty =
|
||||
DependencyProperty.RegisterAttached("ComboBoxColumnStyle",
|
||||
typeof(Style), typeof(DataGridAssist),
|
||||
new PropertyMetadata(null, OnComboBoxColumnStyleChanged));
|
||||
|
||||
private static void OnComboBoxColumnStyleChanged(DependencyObject d,
|
||||
DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var grid = (DataGrid)d;
|
||||
if (e.OldValue == null && e.NewValue != null)
|
||||
{
|
||||
UpdateComboBoxColumnStyles(grid);
|
||||
}
|
||||
}
|
||||
|
||||
private static void UpdateComboBoxColumnStyles(DataGrid grid)
|
||||
{
|
||||
var comboBoxColumnStyle = GetComboBoxColumnStyle(grid);
|
||||
|
||||
foreach (var column in grid.Columns.OfType<DataGridComboBoxColumn>())
|
||||
{
|
||||
var comboBoxElementStyle = new Style
|
||||
{
|
||||
BasedOn = column.ElementStyle,
|
||||
TargetType = comboBoxColumnStyle.TargetType
|
||||
};
|
||||
|
||||
foreach (var setter in comboBoxColumnStyle.Setters.OfType<Setter>())
|
||||
{
|
||||
comboBoxElementStyle.Setters.Add(setter);
|
||||
}
|
||||
|
||||
column.ElementStyle = comboBoxElementStyle;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 编辑ComboBox列
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的编辑组合框列样式。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取样式的依赖对象。</param>
|
||||
/// <returns>返回指定依赖对象的编辑组合框列样式;如果没有设置,则返回默认值。</returns>
|
||||
public static Style GetEditingComboBoxColumnStyle(DependencyObject obj)
|
||||
{
|
||||
return (Style)obj.GetValue(EditingComboBoxColumnStyleProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的编辑组合框列样式。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置样式的依赖对象。</param>
|
||||
/// <param name="value">要应用到依赖对象上的样式。</param>
|
||||
public static void SetEditingComboBoxColumnStyle(DependencyObject obj, Style value)
|
||||
{
|
||||
obj.SetValue(EditingComboBoxColumnStyleProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于设置或获取DataGrid中ComboBox列在编辑状态下的样式。通过此属性,可以自定义当用户开始编辑某ComboBox列时该列的外观。
|
||||
/// 此属性允许开发者指定一个Style对象,该对象定义了ComboBox列在编辑模式下的视觉表现,如背景色、字体等。
|
||||
/// </summary>
|
||||
// Using a DependencyProperty as the backing store for EditingComboBoxColumnStyle.
|
||||
public static readonly DependencyProperty EditingComboBoxColumnStyleProperty =
|
||||
DependencyProperty.RegisterAttached("EditingComboBoxColumnStyle",
|
||||
typeof(Style), typeof(DataGridAssist),
|
||||
new PropertyMetadata(null, OnEditingComboBoxColumnStyleChanged));
|
||||
|
||||
private static void OnEditingComboBoxColumnStyleChanged(DependencyObject d,
|
||||
DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var grid = (DataGrid)d;
|
||||
if (e.OldValue == null && e.NewValue != null)
|
||||
{
|
||||
UpdateEditingComboBoxColumnStyles(grid);
|
||||
}
|
||||
}
|
||||
|
||||
private static void UpdateEditingComboBoxColumnStyles(DataGrid grid)
|
||||
{
|
||||
var editingComboBoxColumnStyle = GetEditingComboBoxColumnStyle(grid);
|
||||
|
||||
foreach (var column in grid.Columns.OfType<DataGridComboBoxColumn>())
|
||||
{
|
||||
var comboBoxElementStyle = new Style
|
||||
{
|
||||
BasedOn = column.EditingElementStyle,
|
||||
TargetType = editingComboBoxColumnStyle.TargetType
|
||||
};
|
||||
|
||||
foreach (var setter in editingComboBoxColumnStyle.Setters.OfType<Setter>())
|
||||
{
|
||||
comboBoxElementStyle.Setters.Add(setter);
|
||||
}
|
||||
|
||||
column.EditingElementStyle = comboBoxElementStyle;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
323
Melskin/Assists/InputAssist.cs
Normal file
323
Melskin/Assists/InputAssist.cs
Normal file
@@ -0,0 +1,323 @@
|
||||
using System.Windows.Controls.Primitives;
|
||||
|
||||
using Melskin.Extensions;
|
||||
|
||||
namespace Melskin.Assists;
|
||||
|
||||
/// <summary>
|
||||
/// InputAssist 类提供了一系列附加属性,用于增强 WPF 应用程序中输入控件的功能。这些功能包括设置占位符文本、占位符颜色、前缀和后缀内容等,支持 TextBoxBase、PasswordBox 和 ComboBox 控件。
|
||||
/// </summary>
|
||||
public static class InputAssist
|
||||
{
|
||||
#region Attached Properties
|
||||
|
||||
/// <summary>
|
||||
/// 用于设置或获取控件的占位符文本。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty PlaceholderTextProperty =
|
||||
DependencyProperty.RegisterAttached("PlaceholderText", typeof(string), typeof(InputAssist), new PropertyMetadata(string.Empty));
|
||||
|
||||
/// <summary>
|
||||
/// 获取输入控件的占位符文本。
|
||||
/// </summary>
|
||||
[AttachedPropertyBrowsableForType(typeof(TextBoxBase))]
|
||||
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
|
||||
[AttachedPropertyBrowsableForType(typeof(ComboBox))]
|
||||
public static string GetPlaceholderText(DependencyObject obj)
|
||||
{
|
||||
return (string)obj.GetValue(PlaceholderTextProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置输入控件的占位符。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置占位符的依赖对象。</param>
|
||||
/// <param name="value">占位符的文本值。</param>
|
||||
public static void SetPlaceholderText(DependencyObject obj, string value)
|
||||
{
|
||||
obj.SetValue(PlaceholderTextProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于设置或获取控件的前缀内容。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty PrefixProperty =
|
||||
DependencyProperty.RegisterAttached("Prefix", typeof(object), typeof(InputAssist), new PropertyMetadata(null));
|
||||
|
||||
/// <summary>
|
||||
/// 获取与指定依赖对象关联的前缀。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取前缀的依赖对象。</param>
|
||||
/// <returns>与指定依赖对象关联的前缀对象。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(TextBox))]
|
||||
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
|
||||
[AttachedPropertyBrowsableForType(typeof(ComboBox))]
|
||||
public static object GetPrefix(DependencyObject obj)
|
||||
{
|
||||
return obj.GetValue(PrefixProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置输入控件的前缀。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置前缀的依赖对象。</param>
|
||||
/// <param name="value">要设置为前缀的对象。</param>
|
||||
public static void SetPrefix(DependencyObject obj, object value)
|
||||
{
|
||||
obj.SetValue(PrefixProperty, value);
|
||||
}
|
||||
// 这是一个只读的依赖属性,用于向XAML报告PasswordBox是否有文本。
|
||||
// 只有这个类内部可以通过Key来修改它的值,保证了状态的可靠性。
|
||||
private static readonly DependencyPropertyKey HasTextPropertyKey =
|
||||
DependencyProperty.RegisterAttachedReadOnly(
|
||||
"HasPassword",
|
||||
typeof(bool),
|
||||
typeof(InputAssist),
|
||||
new PropertyMetadata(false));
|
||||
|
||||
/// <summary>
|
||||
/// 用于获取或设置是否显示密码框内有内容的指示器。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty HasPasswordProperty = HasTextPropertyKey.DependencyProperty;
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定控件是否启用了密码模式。
|
||||
/// </summary>
|
||||
/// <param name="obj">要检查的DependencyObject对象。</param>
|
||||
/// <returns>如果控件启用了密码模式,则返回true;否则返回false。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
|
||||
public static bool GetHasPassword(DependencyObject obj)
|
||||
{
|
||||
return (bool)obj.GetValue(HasPasswordProperty);
|
||||
}
|
||||
|
||||
|
||||
// 核心更新逻辑
|
||||
private static void UpdateHasPassword(PasswordBox passwordBox)
|
||||
{
|
||||
// 根据密码长度是否大于0,来设置HasText属性的值
|
||||
passwordBox.SetValue(HasTextPropertyKey, passwordBox.Password.Length > 0);
|
||||
}
|
||||
/// <summary>
|
||||
/// 用于设置或获取附着到控件上的后缀内容。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty SuffixProperty =
|
||||
DependencyProperty.RegisterAttached("Suffix", typeof(object), typeof(InputAssist), new PropertyMetadata(null));
|
||||
|
||||
/// <summary>
|
||||
/// 获取附加到指定控件的后缀内容。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取后缀内容的DependencyObject对象。</param>
|
||||
/// <returns>返回控件的后缀内容,如果未设置则为null。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(TextBox))]
|
||||
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
|
||||
public static object GetSuffix(DependencyObject obj)
|
||||
{
|
||||
return obj.GetValue(SuffixProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置输入控件的后缀。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置后缀的依赖对象。</param>
|
||||
/// <param name="value">要设置为后缀的对象。</param>
|
||||
public static void SetSuffix(DependencyObject obj, object value)
|
||||
{
|
||||
obj.SetValue(SuffixProperty, value);
|
||||
}
|
||||
|
||||
#region PasswordBox
|
||||
|
||||
/// <summary>
|
||||
/// 用于设置或获取控件的密码文本。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty PasswordProperty =
|
||||
DependencyProperty.RegisterAttached("Password", typeof(string), typeof(InputAssist),
|
||||
new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPasswordChanged));
|
||||
|
||||
private static void OnPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is PasswordBox passwordBox)
|
||||
{
|
||||
// 解除再绑定,防止重复订阅
|
||||
passwordBox.PasswordChanged -= PasswordBox_PasswordChanged;
|
||||
|
||||
var newVal = (string)e.NewValue ?? string.Empty;
|
||||
if (passwordBox.Password != newVal)
|
||||
{
|
||||
passwordBox.Password = newVal;
|
||||
}
|
||||
UpdateHasPassword(passwordBox);
|
||||
// 确保监听用户输入
|
||||
passwordBox.PasswordChanged += PasswordBox_PasswordChanged;
|
||||
}
|
||||
}
|
||||
|
||||
private static void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (sender is not PasswordBox box) return;
|
||||
var attached = GetPassword(box);
|
||||
var actual = box.Password;
|
||||
|
||||
// 当用户输入变化时同步回附加属性
|
||||
if (attached != actual)
|
||||
{
|
||||
// 这里调用 SetPassword 会再次触发 OnPasswordChanged,但因值相同不会再写入造成循环
|
||||
SetPassword(box, actual);
|
||||
}
|
||||
UpdateHasPassword(box);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取输入控件的密码文本。
|
||||
/// </summary>
|
||||
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
|
||||
public static string GetPassword(DependencyObject obj)
|
||||
{
|
||||
return (string)obj.GetValue(PasswordProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置输入控件的密码文本。
|
||||
/// </summary>
|
||||
public static void SetPassword(DependencyObject obj, string value)
|
||||
{
|
||||
obj.SetValue(PasswordProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 切换可见密码的样板的作用
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsPasswordVisibleProperty =
|
||||
DependencyProperty.RegisterAttached("IsPasswordVisible", typeof(bool), typeof(InputAssist), new PropertyMetadata(false));
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the password currently held by PasswordBox is displayed in text.
|
||||
/// </summary>
|
||||
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
|
||||
public static bool GetIsPasswordVisible(DependencyObject obj)
|
||||
{
|
||||
return (bool)obj.GetValue(IsPasswordVisibleProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置是否可以切换密码框的显示模式(显示/隐藏密码)。
|
||||
/// </summary>
|
||||
/// <param name="obj">依赖对象,通常是PasswordBox。</param>
|
||||
/// <param name="value">布尔值,表示是否启用显示/隐藏密码的功能。</param>
|
||||
public static void SetIsPasswordVisible(DependencyObject obj, bool value)
|
||||
{
|
||||
obj.SetValue(IsPasswordVisibleProperty, value);
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 用于设置或获取控件是否可以被清空。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ClearEnabledProperty =
|
||||
DependencyProperty.RegisterAttached("ClearEnabled", typeof(bool), typeof(InputAssist), new PropertyMetadata(false, OnClearEnabledChanged));
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定控件是否启用了清除功能。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取属性的依赖对象。</param>
|
||||
/// <returns>如果启用了清除功能,则返回 true;否则返回 false。</returns>
|
||||
[AttachedPropertyBrowsableForType(typeof(TextBox))]
|
||||
[AttachedPropertyBrowsableForType(typeof(PasswordBox))]
|
||||
[AttachedPropertyBrowsableForType(typeof(ComboBox))]
|
||||
public static bool GetClearEnabled(DependencyObject obj)
|
||||
{
|
||||
return (bool)obj.GetValue(ClearEnabledProperty);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 设置输入控件是否可以被清空。
|
||||
/// </summary>
|
||||
public static void SetClearEnabled(DependencyObject obj, bool value)
|
||||
{
|
||||
obj.SetValue(ClearEnabledProperty, value);
|
||||
}
|
||||
|
||||
private static void OnClearEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is not UIElement element) return;
|
||||
// 根据新值订阅或取消订阅鼠标左键释放事件
|
||||
if ((bool)e.NewValue)
|
||||
{
|
||||
element.MouseLeftButtonUp += OnClear;
|
||||
}
|
||||
else
|
||||
{
|
||||
element.MouseLeftButtonUp -= OnClear;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于设置或获取控件是否显示清除按钮。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ClearableProperty = DependencyProperty.RegisterAttached(
|
||||
"Clearable", typeof(bool), typeof(InputAssist), new PropertyMetadata(default(bool)));
|
||||
|
||||
/// <summary>
|
||||
/// 设置输入控件是否可清除。
|
||||
/// </summary>
|
||||
/// <param name="d">要设置属性的依赖对象。</param>
|
||||
/// <param name="value">布尔值,表示输入控件是否可以被用户清除其内容。</param>
|
||||
public static void SetClearable(DependencyObject d, bool value)
|
||||
{
|
||||
d.SetValue(ClearableProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的可清除属性值。
|
||||
/// </summary>
|
||||
/// <param name="d">要获取属性值的依赖对象。</param>
|
||||
/// <returns>返回一个布尔值,表示该控件是否可以被清除内容。</returns>
|
||||
public static bool GetClearable(DependencyObject d)
|
||||
{
|
||||
return (bool)d.GetValue(ClearableProperty);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
/// <summary>
|
||||
/// 清除事件的处理程序
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private static void OnClear(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (sender is not DependencyObject d) return;
|
||||
//查找父级控件中第一个是 TextBox、PasswordBox 或 ComboBox 的控件
|
||||
var parent = d.GetAncestors().FirstOrDefault(a => a is TextBox or PasswordBox or ComboBox);
|
||||
if (parent != null && !GetClearable(parent)) return;
|
||||
switch (parent)
|
||||
{
|
||||
case TextBox textBox:
|
||||
textBox.Clear();
|
||||
textBox.GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
|
||||
break;
|
||||
case PasswordBox passwordBox:
|
||||
passwordBox.Clear();
|
||||
passwordBox.GetBindingExpression(PasswordProperty)?.UpdateSource();
|
||||
break;
|
||||
case ComboBox comboBox:
|
||||
{
|
||||
if (comboBox.IsEditable)
|
||||
{
|
||||
comboBox.Text = string.Empty;
|
||||
comboBox.GetBindingExpression(ComboBox.TextProperty)?.UpdateSource();
|
||||
}
|
||||
|
||||
comboBox.SelectedItem = null;
|
||||
comboBox.GetBindingExpression(Selector.SelectedItemProperty)?.UpdateSource();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
175
Melskin/Assists/ShadingAssist.cs
Normal file
175
Melskin/Assists/ShadingAssist.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
namespace Melskin.Assists;
|
||||
|
||||
/// <summary>
|
||||
/// ShadingAssist 类提供了一种方式,用于通过依赖属性来设置UI元素的阴影和高亮效果。此类允许开发者为指定的UI元素定义光刷、暗刷、禁用背景色、禁用前景色以及高亮边框颜色。
|
||||
/// 该类继承自DependencyObject,确保了其可以参与到WPF的数据绑定系统中,从而动态地改变UI元素的视觉效果。
|
||||
/// </summary>
|
||||
public class ShadingAssist
|
||||
{
|
||||
#region LightBrush
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的浅阴影效果颜色刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取浅阴影颜色刷的依赖对象。</param>
|
||||
/// <returns>表示浅阴影效果的颜色刷。</returns>
|
||||
public static SolidColorBrush GetLightShadowBrush(DependencyObject obj)
|
||||
{
|
||||
return (SolidColorBrush)obj.GetValue(LightShadowBrushProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 为指定的DependencyObject设置表示浅阴影效果的颜色刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置浅阴影颜色刷的依赖对象。</param>
|
||||
/// <param name="value">将要应用到指定对象上的浅阴影SolidColorBrush。</param>
|
||||
public static void SetLightShadowBrush(DependencyObject obj, SolidColorBrush value)
|
||||
{
|
||||
obj.SetValue(LightShadowBrushProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于获取或设置指定UI元素的浅阴影画刷颜色。此依赖属性允许开发者定义一个SolidColorBrush对象来作为UI元素的浅阴影效果,从而增强视觉层次感。
|
||||
/// 默认值为白色(Brushes.White)。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty LightShadowBrushProperty =
|
||||
DependencyProperty.RegisterAttached("LightShadowBrush",
|
||||
typeof(SolidColorBrush), typeof(ShadingAssist),
|
||||
new PropertyMetadata(Brushes.White));
|
||||
|
||||
#endregion
|
||||
|
||||
#region ShadowBrush
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的深阴影效果颜色刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取深阴影颜色刷的依赖对象。</param>
|
||||
/// <returns>表示深阴影效果的颜色刷。</returns>
|
||||
public static SolidColorBrush GetDarkShadowBrush(DependencyObject obj)
|
||||
{
|
||||
return (SolidColorBrush)obj.GetValue(DarkShadowBrushProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 为指定的依赖对象设置深阴影效果颜色刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要为其设置深阴影颜色刷的依赖对象。</param>
|
||||
/// <param name="value">表示深阴影效果的颜色刷。</param>
|
||||
public static void SetDarkShadowBrush(DependencyObject obj, SolidColorBrush value)
|
||||
{
|
||||
obj.SetValue(DarkShadowBrushProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于获取或设置指定UI元素的深阴影画刷颜色。此依赖属性允许开发者定义一个SolidColorBrush对象来作为UI元素的深阴影效果,从而增强视觉层次感。
|
||||
/// 默认值为黑色(Brushes.Black)。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty DarkShadowBrushProperty =
|
||||
DependencyProperty.RegisterAttached("DarkShadowBrush",
|
||||
typeof(SolidColorBrush), typeof(ShadingAssist),
|
||||
new PropertyMetadata(Brushes.Black));
|
||||
|
||||
#endregion
|
||||
|
||||
#region DisabledBackground
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的禁用背景色。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取禁用背景色的依赖对象。</param>
|
||||
/// <returns>表示禁用状态下的背景颜色刷。</returns>
|
||||
public static Brush GetDisabledBackground(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(DisabledBackgroundProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 为指定的依赖对象设置禁用状态下的背景色。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置禁用背景色的依赖对象。</param>
|
||||
/// <param name="value">新的禁用背景色刷。</param>
|
||||
public static void SetDisabledBackground(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(DisabledBackgroundProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于获取或设置指定UI元素在禁用状态下的背景色。此依赖属性允许开发者定义一个Brush对象来作为UI元素处于不可用状态时的背景颜色,有助于用户直观地识别出元素当前不可交互。
|
||||
/// 默认值为灰色(Brushes.Gray)。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty DisabledBackgroundProperty =
|
||||
DependencyProperty.RegisterAttached("DisabledBackground",
|
||||
typeof(Brush), typeof(ShadingAssist),
|
||||
new PropertyMetadata(Brushes.Gray));
|
||||
|
||||
#endregion
|
||||
|
||||
#region DisabledForeground
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的禁用状态下的前景色刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取禁用前景色刷的依赖对象。</param>
|
||||
/// <returns>表示禁用状态下前景效果的颜色刷。</returns>
|
||||
public static Brush GetDisabledForeground(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(DisabledForegroundProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的禁用状态前景色。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置禁用状态前景色的依赖对象。</param>
|
||||
/// <param name="value">表示禁用状态前景色的画刷。</param>
|
||||
public static void SetDisabledForeground(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(DisabledForegroundProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于获取或设置指定UI元素在禁用状态下的前景色。此依赖属性允许开发者定义一个Brush对象来作为UI元素在不可用状态时的文本或其他前景内容的颜色,以区分于正常状态。
|
||||
/// 默认值为深灰色(Brushes.DarkGray)。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty DisabledForegroundProperty =
|
||||
DependencyProperty.RegisterAttached("DisabledForeground",
|
||||
typeof(Brush), typeof(ShadingAssist),
|
||||
new PropertyMetadata(Brushes.DarkGray));
|
||||
|
||||
#endregion
|
||||
|
||||
#region LightedBorder
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的高亮边框颜色刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取高亮边框颜色刷的依赖对象。</param>
|
||||
/// <returns>表示高亮边框的颜色刷。</returns>
|
||||
public static Brush GetLightedBorder(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(LightedBorderProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的光照边框颜色刷。
|
||||
/// </summary>
|
||||
/// <param name="obj">要设置光照边框颜色刷的依赖对象。</param>
|
||||
/// <param name="value">表示光照边框效果的颜色刷。</param>
|
||||
public static void SetLightedBorder(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(LightedBorderProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于获取或设置指定UI元素的高亮边框颜色。此依赖属性允许开发者定义一个Brush对象来作为UI元素的高亮边框效果,从而增强视觉层次感。
|
||||
/// 默认值为白色(Brushes.White)。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty LightedBorderProperty =
|
||||
DependencyProperty.RegisterAttached("LightedBorder",
|
||||
typeof(Brush), typeof(ShadingAssist),
|
||||
new PropertyMetadata(Brushes.White));
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
146
Melskin/Assists/TabAssist.cs
Normal file
146
Melskin/Assists/TabAssist.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
namespace Melskin.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);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// TabTypeProperty 是一个依赖属性,用于指定 TabControl 中选项卡的样式类型。通过设置此属性为不同的 TabType 枚举值(如 Line 或 Card),可以改变选项卡的外观以适应多样化的用户界面设计需求。默认情况下,该属性被设置为 TabType.Line。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty TabTypeProperty =
|
||||
DependencyProperty.RegisterAttached(
|
||||
"TabType",
|
||||
typeof(TabType),
|
||||
typeof(TabAssist),
|
||||
new PropertyMetadata(TabType.Line)); // 默认值为 Line
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的选项卡类型。
|
||||
/// </summary>
|
||||
/// <param name="element">要设置选项卡类型的依赖对象。</param>
|
||||
/// <param name="value">要设置的选项卡类型,可以是 Line 或 Card。</param>
|
||||
public static void SetTabType(DependencyObject element, TabType value)
|
||||
{
|
||||
element.SetValue(TabTypeProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的选项卡类型。
|
||||
/// </summary>
|
||||
/// <param name="element">要获取选项卡类型的依赖对象。</param>
|
||||
/// <returns>返回依赖对象的选项卡类型,可以是 Line 或 Card。</returns>
|
||||
public static TabType GetTabType(DependencyObject element)
|
||||
{
|
||||
return (TabType)element.GetValue(TabTypeProperty);
|
||||
}
|
||||
|
||||
// 注册 IsEditable 附加属性
|
||||
/// <summary>
|
||||
/// IsEditableProperty 是一个依赖属性,用于控制 TabControl 的选项卡是否可编辑。通过设置此属性为 true 或 false,可以开启或关闭选项卡的编辑功能。
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsEditableProperty =
|
||||
DependencyProperty.RegisterAttached(
|
||||
"IsEditable",
|
||||
typeof(bool),
|
||||
typeof(TabAssist),
|
||||
new PropertyMetadata(false));
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定依赖对象的选项卡是否可编辑。
|
||||
/// </summary>
|
||||
/// <param name="element">要设置 IsEditable 属性的 DependencyObject。</param>
|
||||
/// <param name="value">如果为 true,则启用选项卡编辑功能;否则,禁用该功能。</param>
|
||||
public static void SetIsEditable(DependencyObject element, bool value)
|
||||
{
|
||||
element.SetValue(IsEditableProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定依赖对象的选项卡是否可编辑。
|
||||
/// </summary>
|
||||
/// <param name="element">要获取选项卡编辑状态的依赖对象。</param>
|
||||
/// <returns>如果选项卡可编辑,则返回 true;否则返回 false。</returns>
|
||||
public static bool GetIsEditable(DependencyObject element)
|
||||
{
|
||||
return (bool)element.GetValue(IsEditableProperty);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// TabType 枚举定义了在 WPF 应用程序中 TabControl 可以使用的不同类型的选项卡样式。此枚举支持两种主要的选项卡风格:Line 和 Card,分别代表线型和卡片式布局。
|
||||
/// 通过设置此枚举值,开发者可以轻松地改变选项卡的外观以适应不同的用户界面设计需求。
|
||||
/// </summary>
|
||||
public enum TabType
|
||||
{
|
||||
/// <summary>
|
||||
/// 表示选项卡类型为线型。当 TabControl 的 TabType 属性设置为此值时,表示选项卡将以线条形式显示。
|
||||
/// </summary>
|
||||
Line, // 默认线条样式
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
Card // 卡片样式
|
||||
}
|
||||
Reference in New Issue
Block a user