WPF如何自定義ProgressBar滾動(dòng)條樣式
一、前言
滾動(dòng)條一般用于加載進(jìn)度,我們?cè)诳匆曨l的時(shí)候或者在瀏覽網(wǎng)頁(yè)的時(shí)候經(jīng)常能看到加載進(jìn)度的頁(yè)面。在程序開發(fā)中,默認(rèn)的進(jìn)度加載樣式可能跟程序風(fēng)格不太一樣,或者加載進(jìn)度的時(shí)候需要更改一下加載的樣式。這個(gè)時(shí)候就需要通過(guò)修改ProgressBar的樣式來(lái)實(shí)現(xiàn)。
二、ProgressBar的基本樣式
ProgressBar的基本樣式很簡(jiǎn)單:
<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"/>
顯示效果:

所有代碼已經(jīng)上傳到github:https://github.com/cmfGit/WpfDemo.git
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
C# 使用 Castle 實(shí)現(xiàn) AOP及如何用 Autofac 集成 Castle
這篇文章主要介紹了C# 使用 Castle 實(shí)現(xiàn) AOP及如何用 Autofac 集成 Castle,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-02-02
C#中使用強(qiáng)制類型實(shí)現(xiàn)字符串和ASCII碼之間的轉(zhuǎn)換
這篇文章主要介紹了C#中使用強(qiáng)制類型實(shí)現(xiàn)字符串和ASCII碼之間的轉(zhuǎn)換,本文還給出了另一種方法,需要的朋友可以參考下2014-08-08
C#使用SqlSugarClient進(jìn)行數(shù)據(jù)訪問(wèn)并實(shí)現(xiàn)了統(tǒng)一的批量依賴注入(示例代碼)
M為 BaseDto 請(qǐng)用C# 給出一個(gè)案例,支持不同表對(duì)應(yīng)不同的業(yè)務(wù)邏輯層,然后不同倉(cāng)儲(chǔ)實(shí)例,不同表的業(yè)務(wù),都實(shí)現(xiàn)統(tǒng)一的批量依賴注入,下面通過(guò)示例給大家演示如何使用SqlSugarClient進(jìn)行數(shù)據(jù)訪問(wèn),并實(shí)現(xiàn)了統(tǒng)一的批量依賴注入,感興趣的朋友跟隨小編一起看看吧2024-05-05
Unity3D動(dòng)態(tài)生成平面網(wǎng)格
這篇文章主要為大家詳細(xì)介紹了Unity3D動(dòng)態(tài)生成平面網(wǎng)格,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
C#中winform控制textbox輸入只能為數(shù)字的方法
這篇文章主要介紹了C#中winform控制textbox輸入只能為數(shù)字的方法,包括使用keyPress事件限制鍵盤輸入以及TextChanged事件限制粘貼等情況,來(lái)實(shí)現(xiàn)控制輸入為數(shù)字的功能,需要的朋友可以參考下2015-01-01
C#實(shí)現(xiàn)簡(jiǎn)單獲取及設(shè)置Session類
這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)單獲取及設(shè)置Session類,涉及C#針對(duì)session的設(shè)置及獲取的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03

