个人服务器网站备案,做场景秀的网站,个人网站备案通过做淘客,网页游戏排行榜 511WPF 完美解决改变指示灯的颜色
原有#xff1a;自己再做WPF页面设计后发现直接去查找页面多个控件嵌套情况下找不到指示灯#xff08;Button实现的#xff0c;详细可以看这篇文章 这里#xff09;#xff0c;具体看看来如何实现
加粗样式思路#xff1a;无论多级嵌套自己再做WPF页面设计后发现直接去查找页面多个控件嵌套情况下找不到指示灯Button实现的详细可以看这篇文章 这里具体看看来如何实现
加粗样式思路无论多级嵌套Grid都能找到指示灯 如何从TabControl 下的TabIten-StackPanel-StackPanel -GroupBox 一般都是一级一级的去找现在直接从Grid出发找为什么要从Grid出发找应为Grid有Children属性方便我们来直接使用
XAML TabControl Margin0,8,0,0 x:NameTab
TabItem MinWidth150 WidthautoTabItem.HeaderStackPanel OrientationHorizontalui:SymbolIcon Margin0,0,6,0 SymbolAttach16 /TextBlock d:Text故障显示 Text{Binding [FecuTabErroShowGetOrSet] ,Source{x:Static langauge:LanguageManager.Instance}}//StackPanel/TabItem.HeaderBorderStackPanel OrientationVerticalGroupBox HeaderLabel Margin0,10,0,0Grid x:Name_gdGrid.RowDefinitionsRowDefinition Height*//Grid.RowDefinitionsGrid.ColumnDefinitionsColumnDefinition Widthauto/ColumnDefinition Width60//Grid.ColumnDefinitionsLabel ContentItem 1 Grid.Row0 Grid.Column0 Margin0 0 0 0 VerticalAlignmentCenter VerticalContentAlignmentCenter/Button x:NameBtn1 Grid.Row0 Grid.Column1 Width25 Height25 Margin20 5 5 5 Button.TemplateControlTemplate TargetTypeButtonGrid!-- 外边框 --Ellipse StrokeGray StrokeThickness2Ellipse.FillRadialGradientBrushGradientStop ColorWhite Offset0/GradientStop ColorGray Offset1//RadialGradientBrush/Ellipse.Fill/Ellipse!-- 内部绿色圆形 --Ellipse Width20 Height20 x:NameelpEllipse.FillRadialGradientBrushGradientStop ColorLightGray Offset0/GradientStop ColorGray Offset1//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 FindChildSystem.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?spm1001.2014.3001.5502