using System.Windows.Controls.Primitives; using VariaStudio.Controls; namespace VariaStudio.Assists; using System.ComponentModel; using System.Windows; using System.Windows.Media; /// /// 提供各种控件的附加属性帮助类。 /// public static class ControlAssist { /// /// 按钮变体附加属性的获取方法。 /// /// /// public static Variant GetVariant(DependencyObject obj) { return (Variant)obj.GetValue(VariantProperty); } /// /// 按钮变体附加属性的设置方法。 /// /// /// public static void SetMyProperty(DependencyObject obj, Variant value) { obj.SetValue(VariantProperty, value); } /// /// 按钮变体附加属性 /// public static readonly DependencyProperty VariantProperty = DependencyProperty.RegisterAttached("Variant", typeof(Variant), typeof(ControlAssist), new PropertyMetadata(Variant.Default)); /// /// 定义一个附加属性 IsFlowLayout (或者叫 UseWrapPanel) /// public static readonly DependencyProperty IsFlowLayoutProperty = DependencyProperty.RegisterAttached( "IsFlowLayout", typeof(bool), typeof(ControlAssist), new PropertyMetadata(false)); /// /// 流式布局 /// /// /// public static void SetIsFlowLayout(DependencyObject element, bool value) { element.SetValue(IsFlowLayoutProperty, value); } /// /// 流式布局附加属性的获取方法。 /// /// /// [AttachedPropertyBrowsableForType(typeof(ListBox))] public static bool GetIsFlowLayout(DependencyObject element) { return (bool)element.GetValue(IsFlowLayoutProperty); } /// /// 获取指定对象的外观类型。 /// /// 要获取其外观类型的依赖对象。 /// 返回指定对象的AppearanceType枚举值,表示其外观类型。 [AttachedPropertyBrowsableForType(typeof(Button))] public static AppearanceType GetAppearanceType(DependencyObject obj) { return (AppearanceType)obj.GetValue(AppearanceTypeProperty); } /// /// 设置指定对象的外观类型。 /// /// 要设置其外观类型的依赖对象。 /// AppearanceType枚举值,表示要设置的外观类型。 public static void SetAppearanceType(DependencyObject obj, AppearanceType value) { obj.SetValue(AppearanceTypeProperty, value); } /// /// 代表按钮控件的外观类型依赖属性。通过此属性,可以设置或获取按钮的视觉样式。 /// 支持的样式包括Primary、Info、Success、Warning和Error,分别对应不同的视觉效果。 /// public static readonly DependencyProperty AppearanceTypeProperty = DependencyProperty.RegisterAttached("AppearanceType", typeof(AppearanceType), typeof(ControlAssist), new PropertyMetadata(AppearanceType.None)); /// /// 获取指定对象的图标。 /// /// 要获取其图标的依赖对象。 /// 返回指定对象的IconElement,表示其图标。 [AttachedPropertyBrowsableForType(typeof(Button))] [AttachedPropertyBrowsableForType(typeof(Menu))] public static IconElement GetIcon(DependencyObject obj) { return (IconElement)obj.GetValue(IconProperty); } /// /// 为指定对象设置图标。 /// /// 要为其设置图标的依赖对象。 /// 要设置的IconElement值。 public static void SetIcon(DependencyObject obj, IconElement value) { obj.SetValue(IconProperty, value); } /// /// 代表按钮控件的图标依赖属性。通过此属性,可以设置或获取按钮上显示的图标。 /// 支持使用IconElement来指定具体的图标样式。 /// public static readonly DependencyProperty IconProperty = DependencyProperty.RegisterAttached("Icon", typeof(IconElement), typeof(ControlAssist), new PropertyMetadata(null)); /// /// 获取指定对象的图标放置位置。 /// /// 要获取其图标放置位置的依赖对象。 /// 返回指定对象的Dock枚举值,表示图标的放置位置。 [AttachedPropertyBrowsableForType(typeof(Button))] public static Dock GetIconPlacement(DependencyObject obj) { return (Dock)obj.GetValue(IconPlacementProperty); } /// /// 设置指定对象的图标放置位置。 /// /// 要设置其图标放置位置的依赖对象。 /// 图标放置位置的值,使用Dock枚举表示。 public static void SetIconPlacement(DependencyObject obj, Dock value) { obj.SetValue(IconPlacementProperty, value); } /// /// 代表按钮控件的图标位置依赖属性。通过此属性,可以设置或获取按钮上图标的放置位置。 /// 支持使用Dock枚举中的值来指定图标相对于按钮文本的位置,例如左对齐、右对齐等。 /// public static readonly DependencyProperty IconPlacementProperty = DependencyProperty.RegisterAttached("IconPlacement", typeof(Dock), typeof(ControlAssist), new PropertyMetadata(Dock.Left)); /// /// 代表按钮控件是否正在运行的依赖属性。通过此属性,可以设置或获取按钮的运行状态。 /// 此属性通常用于指示按钮关联的操作是否正在进行中,例如数据加载或处理过程。 /// public static readonly DependencyProperty IsRunningProperty = DependencyProperty.RegisterAttached("IsRunning", typeof(bool), typeof(ControlAssist), new PropertyMetadata(false)); /// /// 获取指定对象的IsRunning状态。 /// /// 要获取其IsRunning状态的依赖对象。 /// 返回一个布尔值,表示指定对象是否正在运行。 [AttachedPropertyBrowsableForType(typeof(Button))] public static bool GetIsRunning(DependencyObject obj) => (bool)obj.GetValue(IsRunningProperty); /// /// 设置指定对象的运行状态。 /// /// 要设置其运行状态的依赖对象。 /// 布尔值,表示对象是否处于运行状态。 public static void SetIsRunning(DependencyObject obj, bool value) => obj.SetValue(IsRunningProperty, value); /// /// 附加属性,用于为UI元素指定一个字符串形式的标识符(锚点名称)。此属性允许开发者将特定的标识字符串与UI元素关联起来, /// 从而在WPF应用程序中实现基于内容滚动位置自动更新当前高亮显示的锚点列表项等功能。通过设置该属性,可以方便地进行用户界面中的快速导航。 /// public static readonly DependencyProperty AnchorHeaderTextProperty = DependencyProperty.RegisterAttached( "AnchorHeaderText", typeof(string), typeof(ControlAssist), new PropertyMetadata(null)); /// /// 获取指定依赖对象的Header属性值。 /// /// 要获取Header属性值的依赖对象。 /// 返回指定依赖对象的Header属性值,如果未设置则返回null。 [AttachedPropertyBrowsableForType(typeof(Anchor))] public static string GetAnchorHeaderText(DependencyObject obj) { return (string)obj.GetValue(AnchorHeaderTextProperty); } /// /// 为指定依赖对象设置Header属性值。 /// /// 要设置Header属性的依赖对象。 /// 要设置的Header属性值,类型为字符串。 public static void SetAnchorHeaderText(DependencyObject obj, string value) { obj.SetValue(AnchorHeaderTextProperty, value); } /// /// 获取指定对象的Orientation属性值。 /// /// 要获取Orientation属性的对象。 /// 返回对象的Orientation属性值,可以是Horizontal或Vertical。 [AttachedPropertyBrowsableForType(typeof(ListBox))] public static Orientation GetOrientation(DependencyObject obj) { return (Orientation)obj.GetValue(OrientationProperty); } /// /// 设置指定对象的Orientation属性。 /// /// 要设置Orientation属性的对象。 /// 要设置的Orientation值,可以是Horizontal或Vertical。 public static void SetOrientation(DependencyObject obj, Orientation value) { obj.SetValue(OrientationProperty, value); } /// /// 表示选择器控件的方向属性,可以用来设置或获取指定依赖对象的Orientation属性值。该属性支持两种方向:Horizontal(水平)和Vertical(垂直),默认值为Vertical。 /// public static readonly DependencyProperty OrientationProperty = DependencyProperty.RegisterAttached("Orientation", typeof(Orientation), typeof(ControlAssist), new PropertyMetadata(Orientation.Vertical)); /// /// 获取指定依赖对象的SynchronizedScroll属性值。 /// /// 要获取SynchronizedScroll属性值的依赖对象。 /// 返回与指定依赖对象关联的ScrollViewer。如果未设置,则返回null。 public static ScrollViewer GetSynchronizedScroll(DependencyObject obj) { return (ScrollViewer)obj.GetValue(SynchronizedScrollProperty); } /// /// 设置指定依赖对象的SynchronizedScroll属性值。 /// /// 要设置SynchronizedScroll属性的依赖对象。 /// 要设置的ScrollViewer值,用于同步滚动位置。 public static void SetSynchronizedScroll(DependencyObject obj, ScrollViewer value) { obj.SetValue(SynchronizedScrollProperty, value); } /// /// SynchronizedScrollProperty 是一个依赖属性,用于在多个 ScrollViewer 控件之间同步滚动位置。通过此属性,可以设置或获取与特定依赖对象关联的 ScrollViewer 实例,从而实现当一个 ScrollViewer 的滚动位置发生变化时,其他关联的 ScrollViewer 也能自动调整其滚动位置以保持一致。 /// 主要用于在 WPF 应用程序中实现多 ScrollViewer 之间的滚动同步功能,特别适用于需要同时滚动显示不同内容但保持视觉一致性的场景。 /// 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 /// /// 获取指定依赖对象的开关处于关闭状态时显示的文本。 /// /// 要获取关闭状态文本的依赖对象。 /// 返回一个字符串,表示开关处于关闭状态时显示的文本。 [AttachedPropertyBrowsableForType(typeof(ToggleButton))] public static string GetOffText(DependencyObject obj) { return (string)obj.GetValue(OffTextProperty); } /// /// 设置开关控件在关闭状态时显示的文本。 /// /// 依赖对象,通常为开关控件实例。 /// 要设置的文本值。 public static void SetOffText(DependencyObject obj, string value) { obj.SetValue(OffTextProperty, value); } /// /// 用于获取或设置开关处于关闭状态时显示的文本。 /// 默认值为"关"。此依赖属性支持动画、样式和数据绑定等功能。 /// public static readonly DependencyProperty OffTextProperty = DependencyProperty.RegisterAttached("OffText", typeof(string), typeof(ControlAssist), new PropertyMetadata("关")); /// /// 获取指定依赖对象的开关处于开启状态时显示的文本。 /// /// 要获取开启状态文本的依赖对象。 /// 返回一个字符串,表示开关处于开启状态时显示的文本。 [AttachedPropertyBrowsableForType(typeof(ToggleButton))] public static string GetOnText(DependencyObject obj) { return (string)obj.GetValue(OnTextProperty); } /// /// 设置指定依赖对象的开关处于开启状态时显示的文本。 /// /// 要设置开启状态文本的依赖对象。 /// 要设置的文本值。 public static void SetOnText(DependencyObject obj, string value) { obj.SetValue(OnTextProperty, value); } /// /// 用于获取或设置开关处于开启状态时显示的文本。 /// 默认值为"开"。此依赖属性支持动画、样式和数据绑定等功能。 /// public static readonly DependencyProperty OnTextProperty = DependencyProperty.RegisterAttached("OnText", typeof(string), typeof(ControlAssist), new PropertyMetadata("开")); /// /// 获取指定依赖对象的开关是否显示文本的状态。 /// /// 要获取显示文本状态的依赖对象。 /// 返回一个布尔值,表示开关是否显示文本。如果为true,则显示文本;如果为false,则不显示文本。 [AttachedPropertyBrowsableForType(typeof(ToggleButton))] public static bool GetShowText(DependencyObject obj) { return (bool)obj.GetValue(ShowTextProperty); } /// /// 设置指定依赖对象的开关控件是否显示文本。 /// /// 要设置显示文本状态的依赖对象。 /// 布尔值,表示是否显示文本。如果为true,则显示文本;如果为false,则不显示文本。 public static void SetShowText(DependencyObject obj, bool value) { obj.SetValue(ShowTextProperty, value); } /// /// 用于获取或设置是否显示开关状态文本。 /// 默认值为false。此依赖属性支持动画、样式和数据绑定等功能。 /// public static readonly DependencyProperty ShowTextProperty = DependencyProperty.RegisterAttached("ShowText", typeof(bool), typeof(ControlAssist), new PropertyMetadata(false)); /// /// 获取指定依赖对象的开关处于关闭状态时的内容。 /// /// 要获取关闭状态内容的依赖对象。 /// 返回一个对象,表示开关处于关闭状态时显示的内容。 [AttachedPropertyBrowsableForType(typeof(ToggleButton))] public static object GetOffContent(DependencyObject obj) { return (object)obj.GetValue(OffContentProperty); } /// /// 设置指定依赖对象的开关处于关闭状态时显示的内容。 /// /// 要设置关闭状态内容的依赖对象。 /// 要设置为关闭状态时显示的内容。 public static void SetOffContent(DependencyObject obj, object value) { obj.SetValue(OffContentProperty, value); } /// /// 用于获取或设置开关处于关闭状态时显示的内容。 /// 默认值为null。此依赖属性支持动画、样式和数据绑定等功能。 /// public static readonly DependencyProperty OffContentProperty = DependencyProperty.RegisterAttached("OffContent", typeof(object), typeof(ControlAssist), new PropertyMetadata(null)); /// /// 获取指定依赖对象的开关处于开启状态时的内容。 /// /// 要获取开启状态内容的依赖对象。 /// 返回一个对象,表示开关处于开启状态时的内容。 [AttachedPropertyBrowsableForType(typeof(ToggleButton))] public static object GetOnContent(DependencyObject obj) { return (object)obj.GetValue(OnContentProperty); } /// /// 设置指定依赖对象的开关处于开启状态时显示的内容。 /// /// 要设置开启状态内容的依赖对象。 /// 要设置为开启状态时显示的内容,可以是任何类型的对象,如字符串或UI元素。 public static void SetOnContent(DependencyObject obj, object value) { obj.SetValue(OnContentProperty, value); } /// /// 用于获取或设置开关处于开启状态时显示的内容。 /// 默认值为null。此依赖属性支持动画、样式和数据绑定等功能。 /// public static readonly DependencyProperty OnContentProperty = DependencyProperty.RegisterAttached("OnContent", typeof(object), typeof(ControlAssist), new PropertyMetadata(null)); #endregion #region Border /// /// 获取与指定依赖对象关联的几何图形。 /// /// 要获取其几何图形属性的依赖对象。 /// 与指定依赖对象关联的几何图形。 public static Geometry GetGeometry(DependencyObject obj) { return (Geometry)obj.GetValue(GeometryProperty); } /// /// 设置与指定依赖对象关联的几何图形。 /// /// 要设置其几何图形属性的依赖对象。 /// 要与此依赖对象关联的新几何图形值。 public static void SetGeometry(DependencyObject obj, Geometry value) { obj.SetValue(GeometryProperty, value); } /// /// 代表附加到控件的几何图形属性。此依赖属性允许在WPF应用程序中为任意控件设置和获取几何图形。 /// public static readonly DependencyProperty GeometryProperty = DependencyProperty.RegisterAttached("Geometry", typeof(Geometry), typeof(ControlAssist)); /// /// 代表附加到控件的圆角半径属性。此依赖属性允许在WPF应用程序中为任意控件设置和获取圆角半径,影响控件的测量和渲染。 /// public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.RegisterAttached( "CornerRadius", typeof(CornerRadius), typeof(ControlAssist), new FrameworkPropertyMetadata( new CornerRadius(4), FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender )); /// /// 获取与指定依赖对象关联的圆角半径。 /// /// 要获取其圆角半径属性的依赖对象。 /// 与指定依赖对象关联的圆角半径。 [Category("NeuAssists")] public static CornerRadius GetCornerRadius(DependencyObject obj) { return (CornerRadius)obj.GetValue(CornerRadiusProperty); } /// /// 设置与指定依赖对象关联的圆角半径。 /// public static void SetCornerRadius(DependencyObject obj, CornerRadius value) { obj.SetValue(CornerRadiusProperty, value); } #endregion }