码迷,mamicode.com
首页 > Windows程序 > 详细

WPF设计带数字标签显示的进度条

时间:2019-10-17 17:57:39      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:mamicode   sed   alc   roo   translate   str   rmi   idt   val   

先上图

技术图片

1.xaml代码

<Style x:Key="ProgressBarStyle" TargetType="{x:Type ProgressBar}">
        <Setter Property="Foreground" Value="#FF06B025"/>
        <Setter Property="Background" Value="#FFE6E6E6"/>
        <Setter Property="BorderBrush" Value="#FFBCBCBC"/>
        <Setter Property="attach:AttachProperty.Percentage" Value=""></Setter>
        <Setter Property="Height" Value="35"/>
        <Setter Property="Width" Value="600"></Setter>
        <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.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 CornerRadius="18" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"/>
                        <Rectangle RadiusX="18" RadiusY="18" x:Name="PART_Track"/>
                        <Grid x:Name="PART_Indicator" ClipToBounds="True" HorizontalAlignment="Left">
                            <Rectangle RadiusX="18" RadiusY="18" x:Name="Indicator" Fill="{TemplateBinding Foreground}"/>
                            <Label  FontSize="14" VerticalAlignment="Center" Content="{TemplateBinding attach:AttachProperty.Percentage}" HorizontalContentAlignment="Center" HorizontalAlignment="Right" VerticalContentAlignment="Center" Background="Transparent" Foreground="White" Panel.ZIndex="1" Margin="{Binding Margin, ElementName=Animation}"></Label>
                            <Rectangle RadiusX="18" RadiusY="18" x:Name="Animation" Fill="{TemplateBinding Foreground}" RenderTransformOrigin="0.5,0.5">
                                <Rectangle.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Rectangle.RenderTransform>
                            </Rectangle>
                        </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>

2.附加属性

public static string GetPercentage(DependencyObject obj)
        {
            return (string)obj.GetValue(PercentageProperty);
        }

        public static void SetPercentage(DependencyObject obj, string value)
        {
            obj.SetValue(PercentageProperty, value);
        }
        //百分比展示
        // Using a DependencyProperty as the backing store for Percentage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PercentageProperty =
            DependencyProperty.RegisterAttached("Percentage", typeof(string), typeof(AttachProperty), new PropertyMetadata(""));

3.在设计器中使用

<ProgressBar Name="pro" Style="{StaticResource ProgressBarStyle}" ath:AttachProperty.Percentage="{Binding Percentage}" Height="35" Foreground="#ff2791ff" Background="#ffe5e9f0" Width="600" Value="0" Maximum="100"></ProgressBar>

4.逻辑代码

private void Func(object obj)
        {
            for (int i = 0; i <= 100; i++)
            {
                this.pro.Dispatcher.Invoke(new Action(()=> {
                    StyleLibrary.Core.AttachProperty.SetPercentage(pro, i.ToString() + "%");
                    pro.Value = i;
                }));
                Thread.Sleep(100);
            }
        }

//调用
ThreadPool.QueueUserWorkItem(Func,null);

5.知识点:附加属性;绑定;样式;

WPF设计带数字标签显示的进度条

标签:mamicode   sed   alc   roo   translate   str   rmi   idt   val   

原文地址:https://www.cnblogs.com/zhuiyishanran/p/11693125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!