当前位置: 首页 > news >正文

WPF 完美解决改变指示灯的颜色

WPF 完美解决改变指示灯的颜色

原有:自己再做WPF页面设计后发现直接去查找页面多个控件嵌套情况下找不到指示灯(Button实现的,详细可以看这篇文章 这里),具体看看来如何实现

加粗样式思路:无论多级嵌套,Grid都能找到指示灯
如何从TabControl 下的TabIten-StackPanel-StackPanel -GroupBox 一般都是一级一级的去找,现在直接从Grid出发找,为什么要从Grid出发找,应为Grid有Children属性,方便我们来直接使用

XAML

 <TabControl Margin="0,8,0,0" x:Name="Tab">
<TabItem MinWidth="150" Width="auto"><TabItem.Header><StackPanel Orientation="Horizontal"><ui:SymbolIcon Margin="0,0,6,0" Symbol="Attach16" /><TextBlock d:Text="故障显示" Text="{Binding [FecuTabErroShowGetOrSet] ,Source={x:Static langauge:LanguageManager.Instance}}"/></StackPanel></TabItem.Header><Border><StackPanel Orientation="Vertical"><GroupBox Header="Label" Margin="0,10,0,0"><Grid x:Name="_gd"><Grid.RowDefinitions><RowDefinition Height="*"/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="auto"/><ColumnDefinition Width="60"/></Grid.ColumnDefinitions><Label Content="Item 1" Grid.Row="0" Grid.Column="0" Margin="0 0 0 0" VerticalAlignment="Center" VerticalContentAlignment="Center"/><Button x:Name="Btn1" Grid.Row="0" Grid.Column="1" Width="25" Height="25" Margin="20 5 5 5 "><Button.Template><ControlTemplate TargetType="Button"><Grid><!-- 外边框 --><Ellipse Stroke="Gray" StrokeThickness="2"><Ellipse.Fill><RadialGradientBrush><GradientStop Color="White" Offset="0"/><GradientStop Color="Gray" Offset="1"/></RadialGradientBrush></Ellipse.Fill></Ellipse><!-- 内部绿色圆形 --><Ellipse Width="20" Height="20" x:Name="elp"><Ellipse.Fill><RadialGradientBrush><GradientStop Color="LightGray" Offset="0"/><GradientStop Color="Gray" Offset="1"/></RadialGradientBrush></Ellipse.Fill></Ellipse></Grid></ControlTemplate></Button.Template></Button></Grid></GroupBox></StackPanel> </Border></TabItem></TabControl>

CS

 Grid gb = this.FindName(gridName) as Grid;foreach (var child in gb.Children){if (child is System.Windows.Controls.Button btn){if (btn.Name == buttonName){switch (statusEnum){ SetButtonRedColor(btn, btn.Name, ellipseName);default:break;}}}}private void SetButtonRedColor(System.Windows.Controls.Button titleButton, string titleControlName, string tagControlName){System.Windows.Controls.Button button1 = (System.Windows.Controls.Button)this.FindName(titleControlName);System.Windows.Shapes.Ellipse tag = (System.Windows.Shapes.Ellipse)button1.Template.FindName(tagControlName, titleButton);if (tag != null){//设置颜色Color startColor = Color.FromRgb(255, 0, 0);Color endColor = Color.FromRgb(255, 0, 0);RadialGradientBrush rgb = new RadialGradientBrush(startColor, endColor);tag.Fill = rgb;}else{//找元素var template = button1.Template;if (template != null){// 从模板中获取根元素var rootElement = template.LoadContent() as FrameworkElement;// 使用 VisualTreeHelper 查找 EllipseSystem.Windows.Shapes.Ellipse tag1 = FindChild<System.Windows.Shapes.Ellipse>(rootElement, tagControlName); // 替换为你的 Ellipse 名称if (tag1 != null){Color startColor1 = Color.FromRgb(255, 0, 0);Color endColor1 = Color.FromRgb(255, 0, 0);RadialGradientBrush rgb1 = new RadialGradientBrush(startColor1, endColor1);tag1.Fill = rgb1;}}}}

指示灯实现
https://blog.csdn.net/Laity07/article/details/144197550?spm=1001.2014.3001.5502

http://www.lryc.cn/news/508233.html

相关文章:

  • Flutter/Dart:使用日志模块Logger Easier
  • 阿里云云服务器初始化
  • Python中SKlearn的K-means使用详解
  • 红帽RHCE认证适用哪些人学习
  • FFmpeg 框架简介和文件解复用
  • 《Java核心技术I》Swing中的边框
  • MySQL 中的常见错误与排查
  • SQL 查询方式比较:子查询与自连接
  • Linux下学【MySQL】所有常用类型详解( 配实操图 通俗易懂 )
  • Gin-vue-admin(1):环境配置和安装
  • 如何在centos系统上挂载U盘
  • 2024年12月大语言模型最新对比:GPT-4、Claude 3、文心一言等详细评测
  • openjdk17 从C++视角看 String的intern的jni方法JVM_InternString方法被gcc编译器连接
  • day16 python(4)——UnitTest
  • Kafka快速扫描
  • python打包时候遇到问题:ImportError: DLL load failed while importing _ufuncs: 找不到指定的模块
  • 【6】期末复习C#第6套
  • 开源轮子 - EasyExcel01(核心api)
  • SpringBoot3+Vue3开发在线考试系统
  • 2. Kafka入门-开发环境准备
  • Halcon中histo_2dim(Operator)算子原理及应用详解
  • TCP 与 UDP
  • ubuntu 安装更新 ollama新版本
  • Numpy基本操作
  • 本地部署webrtc应用怎么把http协议改成https协议?
  • 基于xiaothink对Wanyv-50M模型进行c-eval评估
  • 使用k6进行kafka负载测试
  • Unity A*算法实现+演示
  • 浏览器要求用户确认 Cookies Privacy(隐私相关内容)是基于隐私法规的要求,VUE 实现,html 代码
  • 如何设计高效的商品系统并提升扩展性:从架构到实践的全方位探索