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

相关推荐
xcLeigh4 小时前
WPF进阶 | WPF 数据绑定进阶:绑定模式、转换器与验证
c#·wpf
学与用2 天前
【deepseek实战】绿色好用,不断网
ai·c#·wpf
的不对不3 天前
WPF基础03——InitializeComponent()函数解释
windows·c#·.net·wpf
军训猫猫头4 天前
61.异步编程1 C#例子 WPF例子
开发语言·c#·wpf
时光追逐者4 天前
一组开源、免费、Metro风格的 WPF UI 控件库
ui·开源·c#·.net·wpf·.netcore·微软技术
军训猫猫头4 天前
58.界面参数传递给Command C#例子 WPF例子
开发语言·ui·c#·wpf
xcLeigh5 天前
WPF基础 | 深入 WPF 事件机制:路由事件与自定义事件处理
c#·wpf
军训猫猫头6 天前
60.await与sleep的原理分析 C#例子 WPF例子
开发语言·ui·c#·wpf
敲代码的TKP7 天前
WPF自定义布局--瀑布布局
wpf
xcLeigh8 天前
WPF基础 | WPF 基础概念全解析:布局、控件与事件
c#·wpf