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

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

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

加粗样式 思路:无论多级嵌套,Grid都能找到指示灯

如何从TabControl 下的TabIten-StackPanel-StackPanel -GroupBox 一般都是一级一级的去找,现在直接从Grid出发找,为什么要从Grid出发找,应为Grid有Children属性,方便我们来直接使用

XAML

csharp 复制代码
 <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

csharp 复制代码
 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 查找 Ellipse
                    System.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

相关推荐
阿湯哥12 小时前
多智能体架构深度解析:企业落地如何选择Skills与SubAgents?
架构·wpf
源之缘-OFD先行者12 小时前
自研 WPF 鸟情图表:性能与灵活的双重突破
wpf
Moqiqiuzi12 小时前
WPF单实例启动
wpf
Moqiqiuzi12 小时前
WPF程序打包成安装包的方法
wpf
码农水水1 天前
国家电网Java面试被问:TCP的BBR拥塞控制算法原理
java·开发语言·网络·分布式·面试·wpf
码农水水1 天前
京东Java面试被问:HTTP/2的多路复用和头部压缩实现
java·开发语言·分布式·http·面试·php·wpf
闻缺陷则喜何志丹2 天前
【C# WPF】TextBox的数据绑定
ui·c#·wpf·mvvm·数据绑定·textbox
码农水水2 天前
得物Java面试被问:大规模数据的分布式排序和聚合
java·开发语言·spring boot·分布式·面试·php·wpf
时光慢煮3 天前
行走在多端之间:基于 Flutter × OpenHarmony 的旅行记录应用实践 —— 旅行详情查看模块解析
flutter·华为·开源·wpf·openharmony
xiaobaishuoAI3 天前
分布式事务实战(Seata 版):解决分布式系统数据一致性问题(含代码教学)
大数据·人工智能·分布式·深度学习·wpf·geo