WPF 控件 (十一、进度条)

一、style

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <SolidColorBrush x:Key="ProgressBar.Progress" Color="#FF06B025"/>
    <SolidColorBrush x:Key="ProgressBar.Background" Color="#FFE6E6E6"/>
    <SolidColorBrush x:Key="ProgressBar.Border" Color="#FFBCBCBC"/>
    <Style x:Key="ProgressBarStyle1" TargetType="{x:Type ProgressBar}">
        <Setter Property="Foreground" Value="{StaticResource ProgressBar.Progress}"/>
        <Setter Property="Background" Value="{StaticResource ProgressBar.Background}"/>
        <Setter Property="BorderBrush" Value="{StaticResource ProgressBar.Border}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ProgressBar}">
                    <Grid x:Name="TemplateRoot">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Determinate"/>
                                <VisualState x:Name="Indeterminate">
                                    <Storyboard RepeatBehavior="Forever">
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Animation" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="0.25"/>
                                            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0.25"/>
                                            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="0.25"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <PointAnimationUsingKeyFrames Storyboard.TargetName="Animation" Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)">
                                            <EasingPointKeyFrame KeyTime="0" Value="-0.5,0.5"/>
                                            <EasingPointKeyFrame KeyTime="0:0:1" Value="0.5,0.5"/>
                                            <EasingPointKeyFrame KeyTime="0:0:2" Value="1.5,0.5"/>
                                        </PointAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border CornerRadius="10" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                        <Rectangle x:Name="PART_Track"/>
                        <Grid x:Name="PART_Indicator" ClipToBounds="true" HorizontalAlignment="Left">
                            <Border x:Name="Indicator" CornerRadius="10" Background="{TemplateBinding Foreground}"/>
                            <Border x:Name="Animation" CornerRadius="10" Background="{TemplateBinding Foreground}" RenderTransformOrigin="0.5,0.5">
                                <Border.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Border.RenderTransform>
                            </Border>
                            <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Value,StringFormat={}{0}%}" Foreground="White" HorizontalAlignment="Center"/>
                        </Grid>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Orientation" Value="Vertical">
                            <Setter Property="LayoutTransform" TargetName="TemplateRoot">
                                <Setter.Value>
                                    <RotateTransform Angle="-90"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsIndeterminate" Value="true">
                            <Setter Property="Visibility" TargetName="Indicator" Value="Collapsed"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ProgressBarStyle2" TargetType="{x:Type ProgressBar}">
        <Setter Property="Foreground" Value="{StaticResource ProgressBar.Progress}"/>
        <Setter Property="Background" Value="{StaticResource ProgressBar.Background}"/>
        <Setter Property="BorderBrush" Value="{StaticResource ProgressBar.Border}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ProgressBar}">
                    <Grid x:Name="TemplateRoot">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Determinate"/>
                                <VisualState x:Name="Indeterminate">
                                    <Storyboard RepeatBehavior="Forever">
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Animation" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="0.25"/>
                                            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0.25"/>
                                            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="0.25"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <PointAnimationUsingKeyFrames Storyboard.TargetName="Animation" Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)">
                                            <EasingPointKeyFrame KeyTime="0" Value="-0.5,0.5"/>
                                            <EasingPointKeyFrame KeyTime="0:0:1" Value="0.5,0.5"/>
                                            <EasingPointKeyFrame KeyTime="0:0:2" Value="1.5,0.5"/>
                                        </PointAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border CornerRadius="10" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                        <Rectangle x:Name="PART_Track"/>
                        <Grid x:Name="PART_Indicator" ClipToBounds="true" HorizontalAlignment="Left">
                            <Border x:Name="Indicator" CornerRadius="10">
                                <Border.Background>
                                    <LinearGradientBrush>
                                        <GradientStop Color="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Foreground}"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                            </Border>
                            
                            <Border x:Name="Animation" CornerRadius="10" RenderTransformOrigin="0.5,0.5">
                                <Border.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Border.RenderTransform>
                                <Border.Background>
                                    <LinearGradientBrush>
                                        <GradientStop Color="Green" Offset="0"/>
                                        <GradientStop Color="Aqua" Offset="0.3"/>
                                        <GradientStop Color="Purple" Offset="0.5"/>
                                        <GradientStop Color="Blue" Offset="0.7"/>
                                        <GradientStop Color="Red" Offset="0.9"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                            </Border>
                            <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Value,StringFormat={}{0}%}" Foreground="White" HorizontalAlignment="Center"/>
                        </Grid>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Orientation" Value="Vertical">
                            <Setter Property="LayoutTransform" TargetName="TemplateRoot">
                                <Setter.Value>
                                    <RotateTransform Angle="-90"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsIndeterminate" Value="true">
                            <Setter Property="Visibility" TargetName="Indicator" Value="Collapsed"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ProgressBarStyle3" TargetType="{x:Type ProgressBar}">
        <Setter Property="Foreground" Value="{StaticResource ProgressBar.Progress}"/>
        <Setter Property="Background" >
            <Setter.Value>
                <LinearGradientBrush>
                    <GradientStop Color="Green" Offset="0"/>
                    <GradientStop Color="Aqua" Offset="0.3"/>
                    <GradientStop Color="Purple" Offset="0.5"/>
                    <GradientStop Color="Blue" Offset="0.7"/>
                    <GradientStop Color="Red" Offset="0.9"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderBrush" Value="{StaticResource ProgressBar.Border}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ProgressBar}">
                    <Grid x:Name="TemplateRoot">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Determinate"/>
                                <VisualState x:Name="Indeterminate">
                                    <Storyboard RepeatBehavior="Forever">
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Animation" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="0.25"/>
                                            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0.25"/>
                                            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="0.25"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <PointAnimationUsingKeyFrames Storyboard.TargetName="Animation" Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)">
                                            <EasingPointKeyFrame KeyTime="0" Value="-0.5,0.5"/>
                                            <EasingPointKeyFrame KeyTime="0:0:1" Value="0.5,0.5"/>
                                            <EasingPointKeyFrame KeyTime="0:0:2" Value="1.5,0.5"/>
                                        </PointAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border CornerRadius="10" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                        <Rectangle x:Name="PART_Track"/>
                        <Grid x:Name="PART_Indicator" ClipToBounds="true" HorizontalAlignment="Left">
                            <Border Opacity="0.3" x:Name="Indicator" CornerRadius="10" Background="{TemplateBinding Foreground}">
                            </Border>

                            <Border Opacity="0.3" x:Name="Animation" CornerRadius="10" RenderTransformOrigin="0.5,0.5" Background="{TemplateBinding Foreground}">
                                <Border.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Border.RenderTransform>
                            </Border>
                            <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Value,StringFormat={}{0}%}" Foreground="White" HorizontalAlignment="Center"/>
                        </Grid>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Orientation" Value="Vertical">
                            <Setter Property="LayoutTransform" TargetName="TemplateRoot">
                                <Setter.Value>
                                    <RotateTransform Angle="-90"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsIndeterminate" Value="true">
                            <Setter Property="Visibility" TargetName="Indicator" Value="Collapsed"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

二、Demo

 <WrapPanel>
            <StackPanel Orientation="Horizontal" Margin="10">
                <TextBlock Text="默认:"/>
                <ProgressBar Width="100" Value="50"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="10">
                <TextBlock Text="修改前景色:"/>
                <ProgressBar Width="100" Value="50" Foreground="Blue"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="10">
                <TextBlock Text="修改背景色:"/>
                <ProgressBar Width="100" Value="50" Foreground="Blue" Background="Black"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="10">
                <TextBlock Text="增加圆角和进度显示:"/>
                <ProgressBar Style="{DynamicResource ProgressBarStyle1}" Width="100" Value="50" Foreground="Purple"  />
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="10">
                <TextBlock Text="修改内部进度样式:"/>
                <ProgressBar Style="{DynamicResource ProgressBarStyle2}" Width="100" Value="70" Foreground="Aqua"  />
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="10">
                <TextBlock Text="修改内部进度样式:"/>
                <ProgressBar Style="{DynamicResource ProgressBarStyle3}" Width="100" Value="70" Foreground="Gray"  />
            </StackPanel>
        </WrapPanel>

三、效果

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: WPF环形虚线进度条是一种以圆形形式展示进度的控件。在WPF中,可以使用Ellipse(椭圆)和Path(路径)来创建环形虚线效果。 首先,我们可以在XAML中添加一个Ellipse元素作为背景圆形,并设置其宽度、高度、填充颜色等属性来定义进度条的样式。 接着,我们可以使用Path元素来创建环形虚线效果。虚线的形状可以通过设置路径数据来定义。例如,可以使用LineSegment(线段)和ArcSegment(弧段)来绘制虚线的路径。 然后,使用DoubleAnimation进行动画效果的实现。可以通过设置动画的起始值和结束值来控制进度条的动态变化。通过设置动画的插值函数,可以使进度条以流畅的方式过渡。 最后,在代码中可以根据实际需要来更新进度条的值,从而实现进度的动态显示。可以通过通过绑定ProgressBar或者自定义一个依赖属性来实现。 总之,WPF环形虚线进度条可以通过控制Ellipse和Path元素的样式、动画以及数值来实现。这种形式的进度条可以提供更加直观的用户体验,让用户清晰地了解任务的进展情况。 ### 回答2: WPF环形虚线进度条是一种用于展示进度的界面元素,其外观呈现环形,并且线条是虚线的效果。以下是实现该效果的方法: 首先,我们需要使用WPF的布局容器来创建一个环形的背景,并将其设置为透明。可以使用Canvas或Grid等容器来实现此目的。 其次,在环形背景上,我们可以使用Path元素来绘制虚线。Path元素可以使用Geometry绘制各种形状,包括虚线。我们可以使用LineGeometry来绘制一条直线,并设置其Stroke属性为虚线样式。 然后,我们可以使用Storyboard动画来控制虚线的显示进度。可以使用DoubleAnimation来控制虚线的起始和结束点,从而实现进度的动画效果。可以根据实际需求调整动画的速度、重复次数等属性。 最后,我们可以根据进度条的值来控制动画的触发。可以使用数据绑定将进度条的值绑定到动画的开始和结束点上,从而实现进度条随数据变化而更新动画进度。 总结来说,实现WPF环形虚线进度条需要使用布局容器、Path元素、动画和数据绑定等技术。通过合理设置这些组件的属性和关联关系,我们可以实现一个漂亮而实用的环形虚线进度条。 ### 回答3: WPF环形虚线进度条是一种在WPF应用程序中展示进度的视觉元素。它通常用于表示某个操作的完成程度或加载进度。环形虚线进度条具有以下特点: 1. 外观:环形虚线进度条呈现为一个圆环,通过一系列由虚线组成的元素来表示进度。它的背景可以是透明的或者设置为特定的颜色。 2. 进度显示:进度条上的虚线根据当前的操作进度进行显示。随着操作进展,虚线的数量和位置会发生变化,以反映出当前的进度。 3. 自定义:WPF环形虚线进度条可以通过样式和模板来进行自定义。可以调整虚线的样式、颜色、厚度等属性,以满足不同的设计需求。 4. 动画效果:在操作进行过程中,环形虚线进度条通常会配合动画效果,如旋转或者渐变变化。这样可以增加进度条的交互性和视觉吸引力。 使用WPF环形虚线进度条可以使用户更直观地了解操作的进度,提高用户体验。它适用于各种需要展示进度的应用场景,如文件上传、下载、数据处理等。通过合理设计,并结合其他视觉元素,可以使环形虚线进度条更加美观和有效地传达进度信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值