网站备案在哪里审批做网站怎样找
网站备案在哪里审批,做网站怎样找,网站设计外包合同,网站实例WPF 中实现基于 INotifyPropertyChanged 的状态灯号显示
在 WPF 中实现基于 INotifyPropertyChanged 的状态灯号显示#xff08;如运行 / 停止、正常 / 异常、在线 / 离线等状态#xff09;#xff0c;核心是通过数据模型的属性变更驱动 UI 中灯号的样式#xff08;颜色、形…WPF 中实现基于INotifyPropertyChanged的状态灯号显示在 WPF 中实现基于INotifyPropertyChanged的状态灯号显示如运行 / 停止、正常 / 异常、在线 / 离线等状态核心是通过数据模型的属性变更驱动 UI 中灯号的样式颜色、形状、提示文本变化结合多线程实时更新状态以下是完整、可直接运行的实现方案。核心思路状态模型定义包含状态类型、灯色、提示文本的属性实现INotifyPropertyChanged通知 UI 更新。灯号 UI用Ellipse椭圆模拟灯号通过StyleDataTrigger绑定状态属性自动切换灯色 / 样式。后台线程模拟实时状态变化如传感器在线状态、设备运行状态更新模型属性触发灯号刷新。一、完整实现代码步骤 1定义状态枚举和数据模型先定义状态类型枚举再创建包含状态属性的模型核心是INotifyPropertyChanged实现。using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace _003WPF_LightState_INotifyPropertyChanged { /// summary /// 设备状态枚举可根据业务扩展 /// /summary public enum DeviceStatus { Running, // 运行中绿色 Stopped, // 已停止灰色 Warning, // 警告黄色 Error // 异常红色 } /// summary /// 状态灯数据模型核心INotifyPropertyChanged /// /summary public class StatusLightModel : INotifyPropertyChanged { private DeviceStatus _currentStatus; private string _statusText; /// summary /// 当前设备状态绑定到UI的核心属性 /// /summary public DeviceStatus CurrentStatus { get _currentStatus; set { if (_currentStatus ! value) { _currentStatus value; OnPropertyChanged(CurrentStatus); // 状态变化时自动更新提示文本 UpdateStatusText(); } } } /// summary /// 状态提示文本如“运行中”“异常” /// /summary public string StatusText { get _statusText; set { if (_statusText ! value) { _statusText value; OnPropertyChanged(StatusText); } } } /// summary /// 根据状态更新提示文本 /// /summary private void UpdateStatusText() { switch (CurrentStatus) { case DeviceStatus.Running: StatusText 运行中; break; case DeviceStatus.Stopped: StatusText 已停止; break; case DeviceStatus.Warning: StatusText 警告负载过高; break; case DeviceStatus.Error: StatusText 异常连接断开; break; default: StatusText 未知状态; break; } } // 启动按钮是否可点击 private bool _StartIsEnable true; /// summary /// 实时数值 /// /summary public bool StartIsEnable { get _StartIsEnable; set { // 只有值变化时才触发通知避免无效更新 if (!_StartIsEnable.Equals(value)) { _StartIsEnable value; OnPropertyChanged(StartIsEnable); // 自动获取属性名CallerMemberName特性 } } } // 停止按钮是否可点击 private bool _StopIsEnable false; /// summary /// 实时数值 /// /summary public bool StopIsEnable { get _StopIsEnable; set { // 只有值变化时才触发通知避免无效更新 if (!_StopIsEnable.Equals(value)) { _StopIsEnable value; OnPropertyChanged(StopIsEnable); // 自动获取属性名CallerMemberName特性 } } } #region INotifyPropertyChanged 实现 public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion } }步骤 2设计灯号 UIMainWindow.xaml用Ellipse绘制灯号通过DataTrigger绑定CurrentStatus属性自动切换灯色和样式Window x:Class_003WPF_LightState_INotifyPropertyChanged.MainWindow xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml xmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:localclr-namespace:_003WPF_LightState_INotifyPropertyChanged mc:Ignorabled Title状态灯实时显示 Height350 Width400 Grid Margin20 StackPanel VerticalAlignmentCenter HorizontalAlignmentCenter !-- 状态灯椭圆模拟 -- Ellipse x:NamestatusLight Width80 Height80 Margin0 0 0 20 !-- 灯号样式根据CurrentStatus自动切换颜色 -- Ellipse.Style Style TargetTypeEllipse !-- 默认样式停止 -- Setter PropertyFill ValueLightGray / Setter PropertyStroke ValueGray / Setter PropertyStrokeThickness Value2 / !-- 运行中绿色 -- Style.Triggers DataTrigger Binding{Binding CurrentStatus} ValueRunning Setter PropertyFill ValueLimeGreen / Setter PropertyStroke ValueDarkGreen / /DataTrigger !-- 警告黄色 -- DataTrigger Binding{Binding CurrentStatus} ValueWarning Setter PropertyFill ValueGold / Setter PropertyStroke ValueDarkGoldenrod / /DataTrigger !-- 异常红色 -- DataTrigger Binding{Binding CurrentStatus} ValueError Setter PropertyFill ValueRed / Setter PropertyStroke ValueDarkRed / !-- 异常时添加闪烁动画 -- Setter PropertyEffect Setter.Value DropShadowEffect ColorRed BlurRadius10 ShadowDepth0 / /Setter.Value /Setter /DataTrigger /Style.Triggers /Style /Ellipse.Style /Ellipse !-- 状态文本显示 -- TextBlock Text{Binding StatusText} FontSize20 HorizontalAlignmentCenter Margin0 0 0 30 / !-- 控制按钮 -- StackPanel OrientationHorizontal Button Content模拟状态变化 Width120 Height40 ClickSimulateStatus_Click IsEnabled{Binding StartIsEnable} / Button Content重置为停止 Width120 Height40 ClickResetStatus_Click IsEnabled{Binding StopIsEnable}/ /StackPanel /StackPanel /Grid /Window步骤 3后台线程模拟状态变化MainWindow.xaml.cs通过后台线程随机切换状态模拟真实场景中的状态更新如设备运行状态、网络连接状态using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace _003WPF_LightState_INotifyPropertyChanged { /// summary /// MainWindow.xaml 的交互逻辑 /// /summary public partial class MainWindow : Window { private Thread _statusThread; private bool _isSimulating false; private StatusLightModel _statusModel; public MainWindow() { InitializeComponent(); // 获取数据模型实例 _statusModel new StatusLightModel(); this.DataContext _statusModel; // 初始状态设为停止 _statusModel.CurrentStatus DeviceStatus.Stopped; _statusModel.StopIsEnable false; _statusModel.StartIsEnable true; // 窗口关闭时停止线程 Closing (s, e) { _statusModel.StopIsEnable false; _statusModel.StartIsEnable true; _isSimulating false; }; } /// summary /// 模拟状态变化按钮点击事件启动后台线程 /// /summary private void SimulateStatus_Click(object sender, RoutedEventArgs e) { if (_isSimulating) return; _isSimulating true; _statusModel.StartIsEnable false; _statusModel.StopIsEnable true; // 创建后台线程模拟状态变化 _statusThread new Thread(SimulateStatusChanges) { IsBackground true, // 后台线程窗口关闭自动终止 Name StatusSimulationThread }; _statusThread.Start(); } /// summary /// 重置状态按钮点击事件 /// /summary private void ResetStatus_Click(object sender, RoutedEventArgs e) { _isSimulating false; _statusModel.CurrentStatus DeviceStatus.Stopped; _statusModel.StopIsEnable false; _statusModel.StartIsEnable true; } /// summary /// 后台线程随机切换设备状态模拟实时状态更新 /// /summary private void SimulateStatusChanges() { Random random new Random(); // 状态数组用于随机选择 DeviceStatus[] statuses new[] { DeviceStatus.Running, DeviceStatus.Warning, DeviceStatus.Error }; while (_isSimulating) { // 随机选择一个状态 DeviceStatus newStatus statuses[random.Next(0, statuses.Length)]; // 直接更新模型属性无需DispatcherINotifyPropertyChanged自动处理线程安全 _statusModel.CurrentStatus newStatus; // 模拟状态持续时间2~5秒随机 Thread.Sleep(random.Next(2000, 5000)); } } } }二、关键功能说明1. 灯号样式逻辑基础样式默认灰色停止状态通过DataTrigger绑定CurrentStatus属性自动切换为绿色运行、黄色警告、红色异常。异常闪烁效果异常状态时添加DropShadowEffect实现红色光晕模拟灯号闪烁的视觉效果如需动态闪烁可添加DoubleAnimation动画。2. 线程安全更新后台线程直接修改_statusModel.CurrentStatus属性无需手动调用Dispatcher因为INotifyPropertyChanged结合 WPF 绑定会自动处理线程安全WPF 4.5 特性。线程控制通过_isSimulating布尔标识优雅停止线程避免Thread.Abort()带来的线程安全问题。三、运行效果程序启动后灯号默认显示灰色停止状态文本显示 “已停止”。点击 “模拟状态变化”后台线程随机切换状态运行 / 警告 / 异常灯号自动切换对应颜色文本同步更新。异常状态时灯号为红色并带有光晕添加动画后会闪烁警告为黄色运行为绿色。点击 “重置为停止”状态恢复为停止灯号变回灰色。总结状态灯显示的核心是通过INotifyPropertyChanged绑定状态属性结合DataTrigger自动切换 UI 样式灯色、动画。后台线程更新状态时直接修改模型属性即可WPF 绑定会自动处理线程安全无需手动调用Dispatcher。可通过扩展DeviceStatus枚举和DataTrigger样式轻松适配更多业务状态如离线、待机等。已经附加代码供大家学习参考