WPF-自定义进度条(条形、环形)

本文详细介绍了如何在WPF中使用几何组合模式创建条形和环形进度条,包括动画效果和自定义模板,以及利用IValueConverter实现动态转换。

目录

1、条形进度条制作

1、环形进度条制作 


1、条形进度条制作

 GeometryCombineMode="Intersect":取交集

            DoubleAnimation mydbAnimation = new DoubleAnimation();
            mydbAnimation.From = -50;
            mydbAnimation.To = 400;
            mydbAnimation.Duration = new Duration(TimeSpan.FromSeconds(6));
            tranForm.BeginAnimation(TranslateTransform.XProperty, mydbAnimation);
        <Rectangle Width="400"
                   Height="20"
                   Margin="190,349,0,0" HorizontalAlignment="Center" VerticalAlignment="Center"
                   Fill="LightGray" />
        <Path Name="path"
              Width="400"
              Height="20" VerticalAlignment="Center" HorizontalAlignment="Center"
              Margin="190,349,0,0"
              Fill="DeepPink">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Intersect">
                    <CombinedGeometry.Geometry1>
                        <RectangleGeometry Rect="0,0,400,20" />
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <RectangleGeometry Rect="0,0,50,20">
                            <RectangleGeometry.Transform>
                                <TranslateTransform x:Name="tranForm" X="-6" />
                            </RectangleGeometry.Transform>
                        </RectangleGeometry>
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>
            </Path.Data>
        </Path>

 


2、环形进度条制作 

重写ProgressBar模板;

MyConvert:继承自IValueConverter接口的转化器;

hc:Arc:使用HandyControl框架的弧形控件;

ArcThicknessUnitPixel空心 、Percent实心;

Stretch="None":关闭伸展

    <Grid>
        <ProgressBar x:Name="probar"
                     Width="120"
                     Height="120"
                     Background="Blue"
                     FontSize="30"
                     FontWeight="Bold"
                     Foreground="Blue"
                     Maximum="1"
                     Minimum="0"
                     SmallChange="0.01"
                     Value="{Binding ElementName=sld, Path=Value}">
            <ProgressBar.Template>
                <ControlTemplate TargetType="ProgressBar">
                    <ControlTemplate.Resources>
                        <local:MyConvert x:Key="tod" />
                    </ControlTemplate.Resources>
                    <Grid>
                        <TextBlock HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Value, StringFormat=p0}" />
                        <hc:Arc ArcThickness="15"
                                ArcThicknessUnit="Pixel"
                                EndAngle="360"
                                Fill="LightGray"
                                StartAngle="0"
                                Stretch="None" />
                        <hc:Arc ArcThickness="15"
                                EndAngle="{TemplateBinding Value,Converter={StaticResource tod}}"
                                Fill="{TemplateBinding Background}"
                                StartAngle="0"
                                Stretch="None" />
                    </Grid>
                </ControlTemplate>
            </ProgressBar.Template>
        </ProgressBar>
        <Slider x:Name="sld"
                Width="400"
                Background="Green"
                Maximum="1"
                Minimum="0" />
    </Grid>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值