2021年7月5日星期一

WPF实现环(圆)形进度条

WPF开发者QQ群: 340500857  | 微信群 -> 进入公众号主页 加入组织

每日一笑

对我们宅男宅女来说,休息日就是在家躺着看剧吃东西,累了再睡一觉,才叫休息日!别问我"休息日怎么不出去逛逛!"哪怕走一步!只要出去了!开门了!那就不叫休息日,那是工作日!

前言 

      需要实现环(圆)形进度条。

欢迎转发、分享、点赞,谢谢大家~。  

效果预览(更多效果请下载源码体验):

一、CircularProgressBar.cs代码如下:

using System;using System.Windows;using System.Windows.Controls;using System.Windows.Media.Animation;namespace WpfCircularProgressBar{ public partial class CircularProgressBar : ProgressBar {  public CircularProgressBar()  {   this.ValueChanged += CircularProgressBar_ValueChanged;  }  void CircularProgressBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)  {   CircularProgressBar bar = sender as CircularProgressBar;   double currentAngle = bar.Angle;   double targetAngle = e.NewValue / bar.Maximum * 359.999;   DoubleAnimation anim = new DoubleAnimation(currentAngle, targetAngle, TimeSpan.FromMilliseconds(500));   bar.BeginAnimation(CircularProgressBar.AngleProperty, anim, HandoffBehavior.SnapshotAndReplace);  }  public double Angle  {   get { return (double)GetValue(AngleProperty); }   set { SetValue(AngleProperty, value); }  }  public static readonly DependencyProperty AngleProperty =   DependencyProperty.Register("Angle", typeof(double), typeof(CircularProgressBar), new PropertyMetadata(0.0));  public double StrokeThickness  {   get { return (double)GetValue(StrokeThicknessProperty); }   set { SetValue(StrokeThicknessProperty, value); }  }  public static readonly DependencyProperty StrokeThicknessProperty =   DependencyProperty.Register("StrokeThickness", typeof(double), typeof(CircularProgressBar), new PropertyMetadata(10.0));  public double BrushStrokeThickness  {   get { return (double)GetValue(BrushStrokeThicknessProperty); }   set { SetValue(BrushStrokeThicknessProperty, value); }  }  public static readonly DependencyProperty BrushStrokeThicknessProperty =   DependencyProperty.Register("BrushStrokeThickness", typeof(double), typeof(CircularProgressBar), new PropertyMetadata(1.0)); }}

二、Style.Xaml代码如下:

 <Style TargetType="local:CircularProgressBar">   <Setter Property="Maximum" Value="100"/>   <Setter Property="StrokeThickness" Value="10"/>   <Setter Property="Foreground" Value="Gray"/>   <Setter Property="Background" Value="#1FA7FC"/>   <Setter Property="Width" Value="100"/>   <Setter Property="Height" Value="100"/>   <Setter Property="Template">    <Setter.Value>     <ControlTemplate TargetType="local:CircularProgressBar">      <Viewbox>       <Canvas Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">        <Path Stroke="{TemplateBinding BorderBrush}"         StrokeThickness="{TemplateBinding BrushStrokeThickness}">         <Path.Data>          <PathGeometry>           <PathFigure StartPoint="50,0">            <ArcSegment SweepDirection="Clockwise"              Size="50,50"              Point="49.999127335374,7.61543361704753E-09"              IsLargeArc="True">            </ArcSegment>           </PathFigure>          </PathGeometry>         </Path.Data>        </Path>        <Path Stroke="{TemplateBinding Background}"         StrokeThickness="{TemplateBinding StrokeThickness}">         <Path.Data>          <PathGeometry>           <PathFigure StartPoint="50,0">            <ArcSegment SweepDirection="Clockwise"              Size="50,50"              Point="{Binding Path=Angle, Converter={StaticResource prConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType=ProgressBar}}"              IsLargeArc="{Binding Path=Angle, Converter={StaticResource isLargeConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType=ProgressBar}}">            </ArcSegment>           </PathFigure>          </PathGeometry>         </Path.Data>        </Path>        <Border Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">         <TextBlock Foreground="{TemplateBinding Foreground}" HorizontalAlignment="Center" VerticalAlignment="Center"          Text="{Binding Path=Value, StringFormat={}{0}%,         RelativeSource={RelativeSource TemplatedParent}}"           FontSize="{TemplateBinding FontSize}"/>        </Border>       </Canvas>      </Viewbox>     </ControlTemplate>    </Setter.Value>   </Setter></Style>

三、MainWindow.xaml......

原文转载:http://www.shaoqun.com/a/848256.html

跨境电商:https://www.ikjzd.com/

刘军:https://www.ikjzd.com/w/1835

asiabill:https://www.ikjzd.com/w/1014

weebly:https://www.ikjzd.com/w/2486


WPF开发者QQ群:340500857|微信群->进入公众号主页加入组织每日一笑对我们宅男宅女来说,休息日就是在家躺着看剧吃东西,累了再睡一觉,才叫休息日!别问我"休息日怎么不出去逛逛!"哪怕走一步!只要出去了!开门了!那就不叫休息日,那是工作日!前言需要实现环(圆)形进度条。欢迎转发、分享、点赞,谢谢大家~。效果预览(更多效果请下载源码体验):一、CircularProgressBar.c
邮乐:https://www.ikjzd.com/w/1776
看90后企二代美女,如何书写纺织产业新时代?:https://www.ikjzd.com/articles/22029
亚马逊品牌分析上新功能 / DHL在非洲推出电商购物APP:https://www.ikjzd.com/articles/22030
在国际贸易领域,小众市场值得你关注!:https://www.ikjzd.com/articles/22031
浅谈90后女生做外贸的发展前景如何?有你的影子?:https://www.ikjzd.com/articles/22033
少妇口述:夫妻4P换妻真实经历:http://lady.shaoqun.com/a/78157.html
强壮的公么让我次次高潮 口述我和公忍不住发生了关系:http://lady.shaoqun.com/m/a/247590.html
我和合租女人的疯狂偷情故事(5/5):http://www.30bags.com/m/a/249590.html
Tik Tok开疆拓土,正在不断刺激体育行业!:https://www.ikjzd.com/articles/146373
7月1日起,从英国发送到欧盟的运送方式有重大变化!:https://www.ikjzd.com/articles/146367
跨境电商前沿观察--7月:https://www.ikjzd.com/articles/146363
一个优秀的亚马逊卖家每天在做些什么呢?:https://www.ikjzd.com/articles/146357

没有评论:

发表评论