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