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

相关推荐
gis开发之家18 小时前
Spring Boot 4 定时任务与异步线程池:从 @Scheduled 到高并发任务编排(生产级实战)
java·spring boot·后端·wpf·spring boot4
clz13145212 天前
如何设计一个优雅可靠的金融级联机接口
wpf
闲云自留地2 天前
云平台存储管理员:Cinder 创建挂载卷 + Swift 分布式存储原理
分布式·wpf·swift
敲代码的嘎仔3 天前
从集群锁失效到异步领券:我用分布式锁 + Redisson + MQ 把领券接口 RT 从 200ms 压到 15ms
java·数据库·redis·分布式·缓存·ai·wpf
闲云自留地4 天前
OpenStack 的身份管家:Keystone 令牌、RBAC 权限模型拆解
wpf·openstack
FuckPatience6 天前
WPF 窗体唤醒,判断是否已存在,存在则弹出
wpf
程序员清风6 天前
从单体应用到微服务:后端系统拆分的基本方法
微服务·架构·wpf
FuckPatience7 天前
WPF 改变根据某一列条件改变行样式
wpf
dalong107 天前
WPF:DataGrid样式
wpf
samble9 天前
从零搭建工业控制系统(二十二):线程安全与并发控制
c#·wpf·并发·mvvm·线程安全·工业控制