1. 程式人生 > 其它 >WPF-進度條的應用例子

WPF-進度條的應用例子

一、以獨立窗體形式開啟,並以單獨的執行緒的簡單封裝

XML程式碼如下

<Window x:Class="PrograssBarNet.ProgressBarPrimary"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PrograssBarNet" mc:Ignorable="d" Title="ProgressBarPrimary" Height="100" Width="900" WindowStartupLocation="CenterScreen" Background="Transparent" Foreground="Transparent" WindowStyle="None" AllowsTransparency="True" Topmost="True"> <
Window.Resources> <ResourceDictionary> <!--進度條--> <SolidColorBrush x:Key="ProgressBar.Progress" Color="#FF2564F9"/> <SolidColorBrush x:Key="ProgressBar.Background" Color="#FFB9B9B9"/> <SolidColorBrush x:Key="ProgressBar.Border"
Color="#FFDEDEDE"/> <Style x:Key="BaseProgressBar" 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="0"/> <Setter Property="Border.CornerRadius" Value="13"/> <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.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="Animation"> <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.TargetProperty="(UIElement.RenderTransformOrigin)" Storyboard.TargetName="Animation"> <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 BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="{TemplateBinding Border.CornerRadius}"/> <Border x:Name="PART_Track" MinWidth="26" CornerRadius="{TemplateBinding Border.CornerRadius}"/> <Grid x:Name="PART_Indicator" ClipToBounds="true" HorizontalAlignment="Left"> <Border x:Name="Indicator" MinWidth="26" Background="{TemplateBinding Foreground}" CornerRadius="{TemplateBinding Border.CornerRadius}"/> <Border x:Name="Animation" MinWidth="26" Background="{TemplateBinding Foreground}" CornerRadius="{TemplateBinding Border.CornerRadius}" RenderTransformOrigin="0.5,0.5"> <Border.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransform/> <TranslateTransform/> </TransformGroup> </Border.RenderTransform> <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,8,0" Foreground="#FFFFFF" FontSize="13"> <Run Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Tag}"/> <Run Text="%"/> </TextBlock> </Border> <!--發光板--> <Border ClipToBounds="True" > <Border Width="{Binding PART_Track.Width}" Name="border1" Margin="-100,0"> <Border.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5" Opacity="0.4"> <GradientStop Color="{TemplateBinding Foreground}" Offset="0"/> <GradientStop Color="#FFD8E4FB" Offset="0"/> <GradientStop Color="{TemplateBinding Foreground}" Offset="1"/> </LinearGradientBrush> </Border.Background> </Border> </Border> </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> <EventTrigger RoutedEvent="UserControl.Loaded"> <BeginStoryboard> <Storyboard> <DoubleAnimation Duration="0:0:2" To="1" Storyboard.TargetName="border1" Storyboard.TargetProperty="Background.GradientStops[1].Offset" RepeatBehavior="Forever" AutoReverse="True"/> </Storyboard> </BeginStoryboard> </EventTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> </Window.Resources> <Border CornerRadius="20" Background="Transparent"> <ProgressBar Name="baseProgressBar" Height="40" Maximum="100" Value="10" Style="{DynamicResource BaseProgressBar}" Tag="10" Background="Transparent" /> </Border> </Window>