怎么做网站推广知乎义乌seo快速排名
怎么做网站推广知乎,义乌seo快速排名,北京的网站建设收费标准,百度免费官网入口WPF 依赖属性 #xff08;自定义控件#xff09;ControlTemplate 上位机状态灯
结合 WPF 的依赖属性#xff08;DependencyProperty#xff09; 和 ControlTemplate#xff08;控件模板#xff09; 实现状态灯#xff0c;是制作通用、可深度定制化控件的标准方案 ——…WPF 依赖属性 自定义控件ControlTemplate 上位机状态灯结合 WPF 的依赖属性DependencyProperty和ControlTemplate控件模板实现状态灯是制作通用、可深度定制化控件的标准方案 —— 依赖属性作为控件的 “数据接口” 支撑绑定 / 样式ControlTemplate 作为 “外观模板” 完全分离 UI 与逻辑。以下是完整、可直接运行的示例包含核心逻辑封装、样式定制、MVVM 绑定等特性。核心设计思路依赖属性定义状态灯的核心属性状态、尺寸、颜色、是否闪烁支持数据绑定和外部样式定制。ControlTemplate纯 XAML 定义灯号的 UI 结构椭圆 文本通过触发器根据依赖属性值自动切换样式 / 动画。样式分离逻辑C#与外观XAML完全解耦可在不修改控件代码的前提下全局替换灯号样式如圆形→方形、颜色自定义。一、完整实现代码无动画、纯纯色步骤 1状态枚举LightStatus.csusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceWPF_依懒属性自定义控件{publicenumLightStatus{Off,// 离线灰色Normal,// 正常绿色Warning,// 警告黄色Error// 异常红色}}步骤 2自定义控件StatusLight.cs—— 依赖属性核心usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;namespaceWPF_依懒属性自定义控件{publicclassStatusLight:Control{#region静态构造函数关联默认样式Framework必须staticStatusLight(){DefaultStyleKeyProperty.OverrideMetadata(typeof(StatusLight),newFrameworkPropertyMetadata(typeof(StatusLight)));}#endregion#region依赖属性定义无动画相关属性// 1. 核心状态属性绑定用publicstaticreadonlyDependencyPropertyCurrentStatusPropertyDependencyProperty.Register(nameof(CurrentStatus),typeof(LightStatus),typeof(StatusLight),newPropertyMetadata(LightStatus.Off));// 2. 灯号尺寸属性publicstaticreadonlyDependencyPropertyLightSizePropertyDependencyProperty.Register(nameof(LightSize),typeof(double),typeof(StatusLight),newPropertyMetadata(60.0));// 3. 各状态颜色属性支持外部定制publicstaticreadonlyDependencyPropertyNormalColorPropertyDependencyProperty.Register(nameof(NormalColor),typeof(Brush),typeof(StatusLight),newPropertyMetadata(newSolidColorBrush(Colors.LimeGreen)));publicstaticreadonlyDependencyPropertyWarningColorPropertyDependencyProperty.Register(nameof(WarningColor),typeof(Brush),typeof(StatusLight),newPropertyMetadata(newSolidColorBrush(Colors.Gold)));publicstaticreadonlyDependencyPropertyErrorColorPropertyDependencyProperty.Register(nameof(ErrorColor),typeof(Brush),typeof(StatusLight),newPropertyMetadata(newSolidColorBrush(Colors.Red)));publicstaticreadonlyDependencyPropertyOffColorPropertyDependencyProperty.Register(nameof(OffColor),typeof(Brush),typeof(StatusLight),newPropertyMetadata(newSolidColorBrush(Colors.LightGray)));#endregion#region依赖属性包装器publicLightStatusCurrentStatus{get(LightStatus)GetValue(CurrentStatusProperty);setSetValue(CurrentStatusProperty,value);}publicdoubleLightSize{get(double)GetValue(LightSizeProperty);setSetValue(LightSizeProperty,value);}publicBrushNormalColor{get(Brush)GetValue(NormalColorProperty);setSetValue(NormalColorProperty,value);}publicBrushWarningColor{get(Brush)GetValue(WarningColorProperty);setSetValue(WarningColorProperty,value);}publicBrushErrorColor{get(Brush)GetValue(ErrorColorProperty);setSetValue(ErrorColorProperty,value);}publicBrushOffColor{get(Brush)GetValue(OffColorProperty);setSetValue(OffColorProperty,value);}#endregion}}步骤 3ControlTemplate 样式Themes/Generic.xaml关键右键Generic.xaml→ 属性 → 生成操作 → 设为「Page」Framework 必须ResourceDictionaryxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:localclr-namespace:WPF_依懒属性自定义控件!-- 状态转文本转换器 --local:StatusToTextConverterx:KeyStatusToTextConverter/!-- 纯色状态灯样式无任何动画 --StyleTargetType{x:Type local:StatusLight}Setter PropertyTemplate Setter.Value ControlTemplate TargetType{x:Type local:StatusLight} Grid StackPanel VerticalAlignmentCenterHorizontalAlignmentCenter !-- 灯号主体纯静态椭圆 -- Ellipsex:NamePART_LightCoreWidth{TemplateBinding LightSize}Height{TemplateBinding LightSize}StrokeThickness2Margin0 0 0 10/ !-- 状态文本 -- TextBlockx:NamePART_StatusTextText{Binding CurrentStatus, RelativeSource{RelativeSource TemplatedParent}, Converter{StaticResource StatusToTextConverter}}FontSize16HorizontalAlignmentCenter/ /StackPanel /Grid !-- 触发器仅切换颜色无动画 -- ControlTemplate.Triggers !-- 离线状态 -- Trigger PropertyCurrentStatusValueOff Setter TargetNamePART_LightCorePropertyFillValue{Binding RelativeSource{RelativeSource ModeTemplatedParent},PathOffColor}/ Setter TargetNamePART_LightCorePropertyStrokeValueGray/ /Trigger !-- 正常状态 -- Trigger PropertyCurrentStatusValueNormal Setter TargetNamePART_LightCorePropertyFillValue{Binding RelativeSource{RelativeSource ModeTemplatedParent},PathNormalColor}/ Setter TargetNamePART_LightCorePropertyStrokeValueDarkGreen/ /Trigger !-- 警告状态 -- Trigger PropertyCurrentStatusValueWarning Setter TargetNamePART_LightCorePropertyFillValue{Binding RelativeSource{RelativeSource ModeTemplatedParent},PathWarningColor}/ Setter TargetNamePART_LightCorePropertyStrokeValueDarkGoldenrod/ /Trigger !-- 异常状态 -- Trigger PropertyCurrentStatusValueError Setter TargetNamePART_LightCorePropertyFillValue{Binding RelativeSource{RelativeSource ModeTemplatedParent},PathErrorColor}/ Setter TargetNamePART_LightCorePropertyStrokeValueDarkRed/ /Trigger /ControlTemplate.Triggers /ControlTemplate /Setter.Value /Setter !-- 默认属性 -- Setter PropertyForegroundValueBlack/ Setter PropertyOffColorValueLightGray/ Setter PropertyNormalColorValueLimeGreen/ Setter PropertyWarningColorValueGold/ Setter PropertyErrorColorValueRed/ Setter PropertyLightSizeValue60//Style/ResourceDictionary步骤 4状态转文本转换器StatusToTextConverter.csusingSystem;usingSystem.Collections.Generic;usingSystem.Globalization;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Data;namespaceWPF_依懒属性自定义控件{publicclassStatusToTextConverter:IValueConverter{publicobjectConvert(objectvalue,TypetargetType,objectparameter,CultureInfoculture){if(valueisLightStatusstatus){switch(status){caseLightStatus.Off:return设备离线;caseLightStatus.Normal:return运行正常;caseLightStatus.Warning:return负载过高;caseLightStatus.Error:return连接异常;default:return未知状态;}}return未知状态;}publicobjectConvertBack(objectvalue,TypetargetType,objectparameter,CultureInfoculture){thrownewNotImplementedException();}}}步骤 5主窗口使用MainWindow.xamlWindowx:ClassWPF_依懒属性自定义控件.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WPF_依懒属性自定义控件mc:IgnorabledTitle.NET Framework 纯色状态灯Height400Width500GridMargin20StackPanelVerticalAlignmentCenterHorizontalAlignmentCenterlocal:StatusLightx:NameDefaultLightLightSize70/!--local:StatusLight LightSize60 NormalColorTeal WarningColorOrange ErrorColorCrimson OffColorGainsboro /--StackPanelOrientationHorizontalButtonContent离线Width80Height40ClickSetOff_Click/ButtonContent正常Width80Height40ClickSetNormal_Click/ButtonContent警告Width80Height40ClickSetWarning_Click/ButtonContent异常Width80Height40ClickSetError_Click//StackPanel/StackPanel/Grid/Window步骤 6主窗口逻辑MainWindow.xaml.csusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;namespaceWPF_依懒属性自定义控件{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublicpartialclassMainWindow:Window{privateThread_simulateThread;privatebool_isSimulating;publicMainWindow(){InitializeComponent();Closing(s,e)_isSimulatingfalse;}// 切换为离线privatevoidSetOff_Click(objectsender,RoutedEventArgse){DefaultLight.CurrentStatusLightStatus.Off;_isSimulatingfalse;}// 切换为正常privatevoidSetNormal_Click(objectsender,RoutedEventArgse){DefaultLight.CurrentStatusLightStatus.Normal;_isSimulatingfalse;}// 切换为警告privatevoidSetWarning_Click(objectsender,RoutedEventArgse){DefaultLight.CurrentStatusLightStatus.Warning;_isSimulatingfalse;}// 切换为异常可选启动模拟状态变化privatevoidSetError_Click(objectsender,RoutedEventArgse){DefaultLight.CurrentStatusLightStatus.Error;if(!_isSimulating){_isSimulatingtrue;_simulateThreadnewThread(SimulateStatus){IsBackgroundtrue,NameStatusSimThread};_simulateThread.Start();}}// 后台模拟状态更新演示依赖属性线程安全privatevoidSimulateStatus(){varrandomnewSystem.Random();while(_isSimulating){Dispatcher.Invoke((){varstatusesnew[]{LightStatus.Off,LightStatus.Normal,LightStatus.Warning,LightStatus.Error};DefaultLight.CurrentStatusstatuses[random.Next(0,4)];});Thread.Sleep(2000);// 2秒切换一次状态}}}}二、核心特性说明1. 纯静态纯色渲染移除所有Storyboard/Animation/Paused相关代码彻底规避动画兼容性问题仅通过Trigger切换Ellipse.Fill/Stroke属性实现状态与颜色的静态绑定。2. 完整的依赖属性能力CurrentStatus核心状态属性支持 MVVM 绑定如绑定到 ViewModel 的状态属性LightSize自定义灯号尺寸适配不同 UI 场景NormalColor/WarningColor/ErrorColor/OffColor支持外部自定义各状态颜色无需修改控件代码。3. .NET Framework 全版本兼容无任何 .NET Core 新增属性如IsPaused无动画相关的只读属性操作编译无错误支持 4.0/4.5/4.7.2 等所有 Framework 主流版本。4. 线程安全演示后台线程通过Dispatcher.Invoke更新依赖属性符合 WPF UI 线程安全规则模拟真实业务场景如传感器数据实时更新状态灯。三、使用与调试要点样式加载验证若控件显示为空白检查Generic.xaml的生成操作是否为「Page」检查静态构造函数中DefaultStyleKey是否正确关联typeof(StatusLight)。自定义扩展如需方形灯号将Ellipse替换为Rectangle并可添加CornerRadius实现圆角如需添加图标在ControlTemplate中新增Image元素通过触发器切换Source如需绑定业务数据将CurrentStatus绑定到 ViewModel 的属性MVVM 场景。清理编译缓存删除bin/obj文件夹后重新生成避免旧代码缓存导致的样式不生效。总结该示例是 .NET Framework 下最简、最稳定的状态灯实现核心是「依赖属性数据驱动 ControlTemplate外观定义」纯静态纯色渲染规避了所有动画相关的兼容问题同时保留控件的可定制性和通用能力关键特性支持状态绑定、颜色定制、尺寸调整、多线程安全更新适配绝大多数业务场景如设备状态监控、系统告警等。