WPF自定义控件-水晶球

cs 复制代码
<UserControl x:Class="Zhaoxi.DeviceDashboard.Components.WaterProgress"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Zhaoxi.DeviceDashboard.Components"
             mc:Ignorable="d" 
             d:DesignHeight="200" d:DesignWidth="200">
    <UserControl.Triggers>
        <EventTrigger RoutedEvent="UserControl.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Duration="0:0:1"
                                     From="0" To="-166"
                                     Storyboard.TargetName="tt1"
                                     Storyboard.TargetProperty="X"
                                     RepeatBehavior="Forever"/>
                    <DoubleAnimation Duration="0:0:2.5"
                                     From="0" To="-166"
                                     Storyboard.TargetName="tt2"
                                     Storyboard.TargetProperty="X"
                                     RepeatBehavior="Forever"/>
                    <DoubleAnimation Duration="0:0:4"
                                     From="0" To="-166"
                                     Storyboard.TargetName="tt3"
                                     Storyboard.TargetProperty="X"
                                     RepeatBehavior="Forever"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </UserControl.Triggers>
    <Viewbox>
        <Grid>
            <Border Width="166" Height="166" CornerRadius="90">
                <Border.Clip>
                    <EllipseGeometry Center="83,83" RadiusX="83" RadiusY="83"/>
                </Border.Clip>

                <Canvas>
                    <Canvas.RenderTransform>
                        <TranslateTransform Y="70" x:Name="tth"/>
                    </Canvas.RenderTransform>
                    <Path Data="M0 10A80 80 0 0 1 83 10A80 80 0 0 0 166 10A80 80 0 0 1 249 10A80 80 0 0 0 332 10L332 182 0 182Z" 
Fill="#4C0b3d90">
                        <Path.RenderTransform>
                            <TranslateTransform X="-50" x:Name="tt1"/>
                        </Path.RenderTransform>
                    </Path>
                    <Path Data="M0 5A80 40 0 0 1 83 5A80 40 0 0 0 166 5A80 40 0 0 1 249 5A80 40 0 0 0 332 5L332 182 0 182Z" 
Fill="#7F0b3d90">
                        <Path.RenderTransform>
                            <TranslateTransform X="-150" x:Name="tt2"/>
                        </Path.RenderTransform>
                    </Path>
                    <Path Data="M0 15A80 120 0 0 1 83 15A80 120 0 0 0 166 15A80 120 0 0 1 249 15A80 120 0 0 0 332 15L332 182 0 182Z" 
Fill="#0b3d90">
                        <Path.RenderTransform>
                            <TranslateTransform X="-30" x:Name="tt3"/>
                        </Path.RenderTransform>
                    </Path>
                </Canvas>
            </Border>

            <Border Width="175" Height="175" CornerRadius="90" 
                BorderThickness="5" BorderBrush="#1FDCF5">
                <Border.Background>
                    <RadialGradientBrush>
                        <GradientStop Color="#44FFFFFF" Offset="1"/>
                        <GradientStop Color="#22FFFFFF" Offset="0.528"/>
                        <GradientStop Color="Transparent"/>
                    </RadialGradientBrush>
                </Border.Background>
            </Border>
        </Grid>
    </Viewbox>
</UserControl>
cs 复制代码
 /// <summary>
 /// WaterProgress.xaml 的交互逻辑
 /// </summary>
 public partial class WaterProgress : UserControl
 {
     public double Value
     {
         get { return (double)GetValue(ValueProperty); }
         set { SetValue(ValueProperty, value); }
     }
     public static readonly DependencyProperty ValueProperty =
         DependencyProperty.Register("Value", typeof(double),
             typeof(WaterProgress),
             new PropertyMetadata(0.0, new PropertyChangedCallback(OnValueChanged)));

     private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
     {
         double newValue = (double)e.NewValue;
         (d as WaterProgress).tth.Y = 160 - newValue * 160 / 100;
     }

     public WaterProgress()
     {
         InitializeComponent();
     }
 }

如何使用

先引入该控件

cs 复制代码
xmlns:c="clr-namespace:Zhaoxi.DeviceDashboard.Components"

然后直接给值

cs 复制代码
<c:WaterProgress Value="54" HorizontalAlignment="Left" Margin="5"/>

效果如下:

相关推荐
枫叶林FYL4 小时前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf
她说彩礼65万9 小时前
WPF 多值转换器
wpf
无心水12 小时前
【分布式利器:金融级】金融级分布式架构开源框架全景解读
人工智能·分布式·金融·架构·开源·wpf·金融级框架
她说彩礼65万12 小时前
WPF 转换器
wpf
WPF工业上位机1 天前
匠心研智造,同心赴新程-WPF硬件通讯之串口&Socket
wpf
爱炸薯条的小朋友1 天前
C#由窗体原子表溢出造成的软件闪退,根本原因补充
开发语言·c#·wpf
晚风一隅2 天前
阿里云盘古存储系统:EB级分布式存储的架构革命与技术突破
wpf
步步为营DotNet2 天前
深挖.NET 11:.NET Aspire 在云原生应用状态管理的创新与实践
云原生·.net·wpf
He BianGu3 天前
【项目】WPF VisionMaster 4.0 项目介绍和开发文档
c#·wpf·流程图·开发文档·机器视觉·visionmaster
He BianGu3 天前
【笔记】在WPF中PriorityBinding的详细介绍
笔记·wpf