Wpf-自定义状态控件

news/2024/6/18 3:49:47 标签: wpf, c#, 开发语言

后端代码

    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>

 


http://www.niftyadmin.cn/n/5446638.html

相关文章

爬虫(七)

1.批量爬取知网数据 lxml:是 Python 的一个功能强大且易用的 XML 和 HTML 处理库。它提供了简单又轻巧的 API,使得解析、构建和操作 XML 和 HTML 文档变得非常方便。lxml 库通常用于处理 XML 和 HTML 文档,例如解析网页、处理配置文件等。openpyxl:是 Python 中用于操作 Ex…

oracle表备份及还原

工作中&#xff0c;经常使用Navicat访问及操作Oracle数据库&#xff0c;备份表非常方便Ctrlc、Ctrlv&#xff1b;最近备份表&#xff0c;发现这种操作有问题&#xff1b;数据表有2条检查&#xff0c;使用Ctrlc、Ctrlv操作&#xff0c;发现新备份的表出现4条检查&#xff0c;再对…

elementUI组件库样式修改整理

一、整体修改样式注意点 避免!important&#xff0c;能使用深度选择器就用深度选择器主题色使用变量&#xff0c;方便后期统一修改&#xff0c;最好新建一个单独的文件&#xff0c;专门用于定义公共变量样式文件尽量放在一个文件里&#xff0c;方便后期维护 二、单独element …

Java集合Collection之LinkedList

LinkeList LinkedList&#xff08;双向链表&#xff09;是一种常见的线性数据结构&#xff0c;但是并不会按线性的顺序存储数据。它由一系列节点组成&#xff0c;每个节点包含数据部分和一个指向下一个节点的引用。相比于数组&#xff0c;链表具有动态大小、插入和删除效率高的…

状态管理@Prop、@Link装饰器

Prop Link 父子组件进行数据同步化 prop 单向同步 只支持string、number、boolean、enum类型 负组件对象类型&#xff0c;总组件是对象类型 不可以是数组、any 不允许子组件初始化 Link双向同步 父子类型一直&#xff1a;string、number、boolean、enum、object、class以及他们…

如何使用命令行查看服务器的核心数和内存大小

在管理服务器时&#xff0c;了解服务器的硬件配置是至关重要的。本文将介绍如何使用命令行来查看服务器的核心数和内存大小&#xff0c;涵盖了常见的 Linux、Windows 和 macOS 操作系统。 Linux 查看核心数 你可以使用下面的命令来查看CPU的物理核心数和逻辑核心数&#xff…

微服务day05(中) -- ES索引库操作

索引库就类似数据库表&#xff0c;mapping映射就类似表的结构。 我们要向es中存储数据&#xff0c;必须先创建“库”和“表”。 2.1.mapping映射属性 mapping是对索引库中文档的约束&#xff0c;常见的mapping属性包括&#xff1a; type&#xff1a;字段数据类型&#xff0c;…

Standard C String Character(标准c字符和字符串)

1. atof 语法&#xff1a; #include<stdlib.h> double atof(const char *str); 功能&#xff1a;将字符串str转换成一个双精度数值并返回结果。参数str必须以有效数字开头&#xff0c;但是允许以"E"或"e"除外的任意非数字字符结尾。 #include<i…