Wpf-自定义状态控件

后端代码

    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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值