WPF指示灯的实现方式
WPF指示灯的实现方式
<Button x:Name="Btn1" Width="25" Height="25" Grid.Row="0" Grid.Column="1" Margin="10 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>
改变指示灯颜色
private void SetButtonGreenColor(System.Windows.Controls.Button titleButton, string titleControlName, string tagControlName)
{
System.Windows.Controls.Button button1 = (System.Windows.Controls.Button)this.FindName(titleControlName);
Ellipse tag = (Ellipse)button1.Template.FindName(tagControlName, titleButton);
Color startColor = Color.FromRgb(144, 238, 144);
Color endColor = Color.FromRgb(0, 128, 0);
RadialGradientBrush rgb = new RadialGradientBrush(startColor, endColor);
tag.Fill = rgb;
}
调用
SetButtonGreenColor(Btn1, "Btn1", "elp");