大佬教程收集整理的这篇文章主要介绍了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="@H_651_120@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代码如下:
<Window x:Class="WpfCircularProgressBar.MainWindow" xmlns="http://scheR_763_11845@as.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://scheR_763_11845@as.microsoft.com/winfx/2006/xaml" xmlns:d="http://scheR_763_11845@as.microsoft.com/expression/blend/2008" xmlns:mc="http://scheR_763_11845@as.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfCircularProgressBar" mc:Ignorable="d" title="@H_651_120@mainWindow" Height="450" Width="800"> <Grid> <UniformGrid> <local:CircularProgressBar BACkground="#21BA9D" Value="{Binding ElementName=CirularSlider,Path=value}" BrushStrokeThickness="2" BorderBrush="LightGray"/> <local:CircularProgressBar BACkground="#E14D5F" BorderBrush="#42ABAC" Value="{Binding ElementName=CirularSlider,Path=value}" BrushStrokeThickness="4"/> <local:CircularProgressBar BACkground="#1FA7FC" BorderBrush="#D6D6D6" Value="{Binding ElementName=CirularSlider,Path=value}" BrushStrokeThickness="10" StrokeThickness="10" Foreground="Black"/> <local:CircularProgressBar Value="{Binding ElementName=CirularSlider,Path=value}"/> <Slider Minimum="0" Maximum="100" x:Name="CirularSlider" issnapToTickEnabled="True" VerticalAlignment="Center" Value="10"/> <Image source="gzh.png"/> </UniformGrid> </Grid> </Window>
更多教程欢迎关注微信公众号:
WPF开发者QQ群: 340500857
blogs: https://www.cnblogs.com/yanjinhua/p/14345136.html
源码Github:https://github.com/yanjinhuagood/WPFDevelopers.git
gitee:https://gitee.com/yanjinhua/WPFDevelopers.git
以上是大佬教程为你收集整理的WPF实现环(圆)形进度条全部内容,希望文章能够帮你解决WPF实现环(圆)形进度条所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。