WPF如何自定义ProgressBar滚动条样式

所属分类: 软件编程 / C#教程 阅读数: 114
收藏 0 赞 0 分享

一、前言

滚动条一般用于加载进度,我们在看视频的时候或者在浏览网页的时候经常能看到加载进度的页面。在程序开发中,默认的进度加载样式可能跟程序风格不太一样,或者加载进度的时候需要更改一下加载的样式。这个时候就需要通过修改ProgressBar的样式来实现。

二、ProgressBar的基本样式

ProgressBar的基本样式很简单:

<Style TargetType="{x:Type ProgressBar}">
   <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
   <Setter Property="SnapsToDevicePixels" Value="True"/>
   <Setter Property="Height" Value="15"/>
   <Setter Property="Background" Value="#6fae5f"/>
   <Setter Property="FontSize" Value="10"/>
   <Setter Property="Padding" Value="5,0"/>
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="{x:Type ProgressBar}">
      <Grid Background="#00000000">
       <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
       </Grid.RowDefinitions>
       <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CommonStates">
         <VisualState x:Name="Determinate"/>
         <VisualState x:Name="Indeterminate">
          <Storyboard RepeatBehavior="Forever">
           <PointAnimationUsingKeyFrames Storyboard.TargetName="Animation" Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)">
            <EasingPointKeyFrame KeyTime="0:0:0" Value="0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:1.5" Value="1.95,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:3" Value="0.5,0.5"/>
           </PointAnimationUsingKeyFrames>
          </Storyboard>
         </VisualState>
        </VisualStateGroup>
       </VisualStateManager.VisualStateGroups>
       
       <Grid Height="{TemplateBinding Height}">
        <Border Background="#000000" CornerRadius="7.5" Opacity="0.05"/>
        <Border BorderBrush="#000000" BorderThickness="1" CornerRadius="7.5" Opacity="0.1"/>
        <Grid Margin="{TemplateBinding BorderThickness}">
         <Border x:Name="PART_Track"/>
         <Grid x:Name="PART_Indicator" ClipToBounds="True" HorizontalAlignment="Left" >
          <Grid.ColumnDefinitions>
           <ColumnDefinition x:Name="width1"/>
           <ColumnDefinition x:Name="width2" Width="0"/>
          </Grid.ColumnDefinitions>
          <Grid x:Name="Animation" RenderTransformOrigin="0.5,0.5">
           <Grid.RenderTransform>
            <TransformGroup>
             <ScaleTransform ScaleY="-1" ScaleX="1"/>
             <SkewTransform AngleY="0" AngleX="0"/>
             <RotateTransform Angle="180"/>
             <TranslateTransform/>
            </TransformGroup>
           </Grid.RenderTransform>
           <Border Background="{TemplateBinding Background}" CornerRadius="7.5">
            <Viewbox HorizontalAlignment="Left" StretchDirection="DownOnly" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="True">
             <TextBlock Foreground="#ffffff" SnapsToDevicePixels="True" FontSize="{TemplateBinding FontSize}" VerticalAlignment="Center" Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Value,StringFormat={}{0}%}" RenderTransformOrigin="0.5,0.5">
              <TextBlock.RenderTransform>
               <TransformGroup>
                <ScaleTransform ScaleY="1" ScaleX="-1"/>
                <SkewTransform AngleY="0" AngleX="0"/>
                <RotateTransform Angle="0"/>
                <TranslateTransform/>
               </TransformGroup>
              </TextBlock.RenderTransform>
             </TextBlock>
            </Viewbox>
           </Border>
           <Border BorderBrush="#000000" BorderThickness="1" CornerRadius="7.5" Opacity="0.1"/>
          </Grid>
         </Grid>
        </Grid>
       </Grid>
      </Grid>
      <ControlTemplate.Triggers>
       
       <Trigger Property="IsEnabled" Value="False">
        <Setter Property="Background" Value="#c5c5c5"/>
       </Trigger>
       <Trigger Property="IsIndeterminate" Value="true">
        <Setter TargetName="width1" Property="Width" Value="0.25*"/>
        <Setter TargetName="width2" Property="Width" Value="0.725*"/>
       </Trigger>
      </ControlTemplate.Triggers>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
  </Style>

引用示例:

<ProgressBar Height="15" Width="150" Value="40" Margin="10"/>

显示效果:

所有代码已经上传到github:https://github.com/cmfGit/WpfDemo.git

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

C# 大小写转换(金额)实例代码

C# 大小写转换(金额)实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

由于在WinForm中Panel不能直接响应鼠标的滚动事件,只好采用捕获窗体的滚动事件。
收藏 0 赞 0 分享

C#访问应用程序配置文件的方法

C#访问应用程序配置文件的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#读写文件的方法汇总

C#读写文件的方法汇总,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#计算代码执行时间的方法

在一些测试工作时我们需要获得高精度的代码执行时间以比较其效率。
收藏 0 赞 0 分享

C# 动画窗体(AnimateWindow)的小例子

C# 动画窗体(AnimateWindow)的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# zxing二维码写入的实例代码

C# zxing二维码写入的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

c#判断输入的是不是数字的小例子

c#判断输入的是不是数字的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

c# 深拷贝与浅拷贝的区别分析及实例

浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用. 深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
收藏 0 赞 0 分享

C#利用com操作excel释放进程的解决方法

最近利用Microsoft.Office.Interop.Excel.Application读取一个excel后,进程中一直存在excel,在网上找了一阵子,其中有几个解决方案
收藏 0 赞 0 分享
查看更多