后端代码
public class AxisStatus : Control
{
static AxisStatus()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(AxisStatus), new FrameworkPropertyMetadata(typeof(AxisStatus)));
}
public CornerRadius CornerRadius
{
get => (CornerRadius)GetValue(CornerRadiusProperty);
set => SetValue(CornerRadiusProperty, value);
}
public static readonly DependencyProperty CornerRadiusProperty =
DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(AxisStatus), new PropertyMetadata(default(CornerRadius)));
public Status Level
{
get => (Status)GetValue(LevelProperty);
set => SetValue(LevelProperty, value);
}
public static readonly DependencyProperty LevelProperty =
DependencyProperty.Register("Level", typeof(Status), typeof(AxisStatus), new PropertyMetadata(Status.Nullable, StatusChangedCallBack));
private static void StatusChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is AxisStatus cus)
{
DoubleAnimation da = new DoubleAnimation()
{
From = 1,
To = 0,
AutoReverse = true,
Duration = TimeSpan.FromSeconds(0.5),
RepeatBehavior = RepeatBehavior.Forever
};
if (e.NewValue != e.OldValue && e.NewValue is Status status)
switch (status)
{
case Status.Nullable:
cus.Background = Brushes.White;
break;
case Status.Ready:
cus.Background = Brushes.Green;
break;
case Status.Run:
cus.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("Green"));
cus.Background.BeginAnimation(Brush.OpacityProperty, da);
break;
case Status.Warning:
cus.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("Yellow"));
cus.Background.BeginAnimation(Brush.OpacityProperty, da);
break;
case Status.Error:
cus.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("Red"));
cus.Background.BeginAnimation(Brush.OpacityProperty, da);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
public ICommand MyCmd
{
get => (ICommand)GetValue(MyCmdProperty);
set => SetValue(MyCmdProperty, value);
}
public static readonly DependencyProperty MyCmdProperty =
DependencyProperty.Register("MyCmd", typeof(ICommand), typeof(AxisStatus), new PropertyMetadata(null));
public static string GetAttach(DependencyObject obj)
{
return (string)obj.GetValue(AttachProperty);
}
public static void SetAttach(DependencyObject obj, string value)
{
obj.SetValue(AttachProperty, value);
}
public static readonly DependencyProperty AttachProperty
= DependencyProperty.RegisterAttached("Attach", typeof(string), typeof(AxisStatus), new PropertyMetadata(null));
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
}
前端代码
<Style TargetType="{x:Type custom:AxisStatus}">
<Setter Property="BorderThickness" Value="3" />
<Setter Property="CornerRadius" Value="30" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Width" Value="45" />
<Setter Property="Height" Value="45" />
<Setter Property="Margin" Value="5,0" />
<Setter Property="Background" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type custom:AxisStatus}">
<Border Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}">
<Border.Effect>
<DropShadowEffect BlurRadius="13"
Direction="0"
ShadowDepth="0"
Color="Black" />
</Border.Effect>
<Border.BorderBrush>
<RadialGradientBrush>
<GradientStop Offset="1" Color="Gray" />
<GradientStop Offset="0" Color="LightGray" />
</RadialGradientBrush>
</Border.BorderBrush>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>