1.Led灯
cs
public class Led : ContentControl
{
#region Member Variables
private Ellipse _ellipse = new Ellipse();
private readonly Color _goodColor;
private readonly Color _warnColor;
private readonly Color _normalColor;
#endregion Member Variables
/// <summary>
/// Led状态
/// </summary>
public static readonly DependencyProperty LedStatusProperty = DependencyProperty.Register("LedStatus", typeof(LedStatus), typeof(Led), new FrameworkPropertyMetadata
{
BindsTwoWayByDefault = true,
DefaultValue = LedStatus.Good,
PropertyChangedCallback = ActiveLedChanged
});
/// <summary>
/// Led与标题间隔
/// </summary>
public static readonly DependencyProperty MarginTProperty = DependencyProperty.Register("MarginT", typeof(double), typeof(Led), new PropertyMetadata(10.0));
/// <summary>
/// Led宽度
/// </summary>
public static readonly DependencyProperty LedWidthProperty = DependencyProperty.Register("LedWidth", typeof(float), typeof(Led), new PropertyMetadata(10.0f));
public static readonly DependencyProperty LedHeightProperty = DependencyProperty.Register("LedHeight", typeof(float), typeof(Led), new PropertyMetadata(10.0f));
public static readonly DependencyProperty OffOpacityProperty = DependencyProperty.Register("OffOpacity", typeof(double), typeof(Led), new PropertyMetadata(1.0));
public static readonly DependencyProperty IsShakeProperty = DependencyProperty.Register("IsShake", typeof(bool), typeof(Led), new FrameworkPropertyMetadata
{
BindsTwoWayByDefault = true,
DefaultValue = false,
PropertyChangedCallback = IsShakeChanged
});
public static readonly DependencyProperty LedOrientationProperty = DependencyProperty.Register("Orientation", typeof(Orientation), typeof(Led), new PropertyMetadata(Orientation.Horizontal));
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(Led), new FrameworkPropertyMetadata
{
BindsTwoWayByDefault = true,
DefaultValue = "",
PropertyChangedCallback = TitleChanged
});
public LedStatus LedStatus
{
get => (LedStatus)GetValue(LedStatusProperty);
set => SetValue(LedStatusProperty, value);
}
public double MarginT
{
get => (double)GetValue(MarginTProperty);
set => SetValue(MarginTProperty, value);
}
public double OffOpacity
{
get => (double)GetValue(OffOpacityProperty);
set => SetValue(OffOpacityProperty, value);
}
public bool IsShake
{
get => (bool)GetValue(IsShakeProperty);
set => SetValue(IsShakeProperty, value);
}
public Orientation LedOrientation
{
get => (Orientation)GetValue(LedOrientationProperty);
set => SetValue(LedOrientationProperty, value);
}
public string Title
{
get => (string)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
public float LedWidth
{
get => (float)GetValue(LedWidthProperty);
set => SetValue(LedWidthProperty, value);
}
public float LedHeight
{
get => (float)GetValue(LedHeightProperty);
set => SetValue(LedHeightProperty, value);
}
private static void TitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!(d is Led led)) return;
led.Title = (string)e.NewValue;
led.LoadLed(d, null);
}
private static void IsShakeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is Led led)
{
//led.IsShake = (bool)e.NewValue;
led.LoadLed(d, null);
//led.LedOn();
}
}
public Led()
{
base.Height = 30.0;
_goodColor = Color.FromArgb(255, 05, 0xe2, 0x5a);
_warnColor = Color.FromArgb(255, 0xe2, 11, 05);
_normalColor = Color.FromArgb(255, 161, 161, 161);
base.Loaded += LoadLed;
}
private void LoadLed(object sender, RoutedEventArgs e)
{
var stackPanel2 = (StackPanel)(base.Content = new StackPanel());
stackPanel2.Margin = base.Margin;
stackPanel2.Orientation = LedOrientation;
stackPanel2.Children.Clear();
_ellipse = new Ellipse()
{
Height = LedHeight,
Width = LedWidth,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
#region 椭圆边框
//var color = GetColor(LedStatus, isBorder: true);
//if (num <= 50.0)
//{
// _ellipse.StrokeThickness = 2.0;
//}
//else if (num <= 100.0)
//{
// _ellipse.StrokeThickness = 4.0;
//}
//else
//{
// _ellipse.StrokeThickness = 6.0;
//}F
//_ellipse.Stroke = color;
#endregion 椭圆边框
_ellipse.Fill = GetColor(LedStatus);
_ellipse.Fill.Opacity = OffOpacity;
//_ellipse.Effect = new DropShadowEffect
//{
// Color = Color.FromRgb(255, 215, 0), // 发光颜色
// BlurRadius = 1, // 模糊半径
// Direction = 320, // 方向(与高光位置匹配)
// Opacity = 0.3, // 透明度
// ShadowDepth = 0 // 阴影深度(0表示纯发光)
//};
添加边框效果
//_ellipse.Stroke = new SolidColorBrush(Color.FromRgb(218, 165, 32));
//_ellipse.StrokeThickness = 0.1;
var textBlock = new TextBlock
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
FontSize = base.FontSize,
Text = Title,
Margin = new Thickness(MarginT, 0.0, 2, 0.0)
};
stackPanel2.Children.Add(_ellipse);
stackPanel2.Children.Add(textBlock);
LedOn();
}
private static void ActiveLedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!(d is Led led)) return;
led.LedStatus = (LedStatus)e.NewValue;
led.LedOn();
}
private Brush GetColor(LedStatus value, bool isBorder = false)
{
Color color;
switch (value)
{
case LedStatus.Normal:
color = _normalColor;
break;
case LedStatus.Good:
color = _goodColor;
break;
case LedStatus.Warn:
color = _warnColor;
break;
default:
throw new ArgumentOutOfRangeException(nameof(value), value, null);
case LedStatus.Error:
color = _warnColor;
break;
}
//var brush = new SolidColorBrush(color);
//return brush;
// 创建径向渐变画笔 - 模拟非中心高光
RadialGradientBrush brush = new RadialGradientBrush
{
GradientOrigin = new Point(0.7, 0.3), // 高光偏移位置
Center = new Point(0.5, 0.5), // 渐变中心
RadiusX = 0.7, // X半径
RadiusY = 0.7, // Y半径
GradientStops = new GradientStopCollection
{
// 高光部分(白色到浅黄色)
new GradientStop(Color.FromArgb(225, 255, 255, 255), 0.0),
new GradientStop(Color.FromArgb(225, 255, 240, 160), 0.1),
// 主体颜色(黄色)
//new GradientStop(Color.FromRgb(255, 215, 0), 0.4),
new GradientStop(Color.FromArgb(250,color.R, color.G, color.B), 0.2),
new GradientStop(Color.FromArgb(150,color.R, color.G, color.B), 0.6),
// 边缘颜色(深黄色到橙色)
//new GradientStop(Color.FromArgb(150, 218, 165, 32), 0.9),
//new GradientStop(Color.FromArgb(120,139, 69, 19), 1.0)
//new GradientStop(Color.FromArgb(150, 67, 65, 63), 0.9),
new GradientStop(Color.FromArgb(12,67, 65, 63), 1.0)
}
};
return brush;
}
private void LedOn()
{
var animation = GetAnimation();
_ellipse.Stroke = GetColor(LedStatus, isBorder: true);
_ellipse.Fill = GetColor(LedStatus);
if (_ellipse.Fill .Opacity < 1.0)
{
_ellipse.Fill.BeginAnimation(Brush.OpacityProperty, animation);
}
}
private void LedOff()
{
var animation = GetAnimation();
_ellipse.Stroke = GetColor(LedStatus, isBorder: true);
_ellipse.Fill = GetColor(LedStatus);
if (_ellipse.Fill != null && _ellipse.Fill.Opacity > OffOpacity)
{
_ellipse.Fill.BeginAnimation(Brush.OpacityProperty, animation);
}
}
private DoubleAnimation GetAnimation()
{
var doubleAnimation = new DoubleAnimation
{
From = 1.0,
To = OffOpacity,
Duration = new Duration(TimeSpan.FromSeconds(0.5)),
AutoReverse = true
};
if (!IsShake) return doubleAnimation;
doubleAnimation.To = 0.0;
doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
return doubleAnimation;
}
}
public enum LedStatus
{
Normal,
Good,
Warn,
Error
}
示例:

2.图片按钮
XML
<Button
Grid.Column="2"
Background="Transparent"
ToolTip="添加">
<Image Source="/Lithography;component/Themes/Images/right2.png" />
</Button>
...未完待续