Files
Shrlalgo.RvKits/Melskin/Assists/ControlAssist.cs
2026-02-17 22:17:13 +08:00

510 lines
21 KiB
C#
Raw Permalink 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.
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
}