wpf 自定义平面进度条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17328557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:05:32  来源:igfitidea点击:

Custom Flat Progress Bar

wpfxamlprogress-bar

提问by Sandeep Bansal

I'm trying to create a progress bar similar to what looks like below, it's in an Indeterminate effect so it'll just be an animation looping over and over.

我正在尝试创建一个类似于下面的进度条,它处于不确定效果中,因此它只是一个一遍又一遍地循环的动画。

enter image description here

在此处输入图片说明

The problem I'm having is where do I begin?

我遇到的问题是从哪里开始?

My main logic on this would be that i need to overlay a rectangle on top of another and then apply some sort of animation to make the top rectangle move and then just loop that same thing.

我对此的主要逻辑是,我需要将一个矩形叠加在另一个矩形的顶部,然后应用某种动画来使顶部矩形移动,然后循环执行相同的操作。

I can do this in a storyboard in Blend but I don't think this is the best way to achieve this.

我可以在 Blend 的故事板中做到这一点,但我认为这不是实现这一目标的最佳方式。

Can anyone point me in the right direction?

任何人都可以指出我正确的方向吗?

EDIT:

编辑:

Working XAML:

工作 XAML:

    <Style x:Key="{x:Type ProgressBar}"
       TargetType="{x:Type ProgressBar}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ProgressBar}">
        <Grid MinHeight="14"
              MinWidth="200"
              Background="#FFF0F0F0">
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
              <VisualState x:Name="Determinate" />
              <VisualState x:Name="Indeterminate">
                <Storyboard>
                  <ObjectAnimationUsingKeyFrames Duration="00:00:00"
                                                 Storyboard.TargetName="PART_Indicator"
                                                 Storyboard.TargetProperty="Background">
                    <DiscreteObjectKeyFrame KeyTime="00:00:00">
                      <DiscreteObjectKeyFrame.Value>
                        <SolidColorBrush>#FFF0F0F0</SolidColorBrush>
                      </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                  </ObjectAnimationUsingKeyFrames>

                </Storyboard>
              </VisualState>
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <Border x:Name="PART_Track"
                  CornerRadius="0"
                  BorderThickness="1">
            <Border.BorderBrush>
              <SolidColorBrush Color="#FFF0F0F0" />
            </Border.BorderBrush>
          </Border>
          <Border x:Name="PART_Indicator"
                  CornerRadius="0"
                  BorderThickness="0"
                  HorizontalAlignment="Left"
                  Background="#FF4B8BC2"
                  Margin="0,-1,0,1">
            <Border.BorderBrush>
              <SolidColorBrush Color="#FFD7D7D7" />

            </Border.BorderBrush>
            <Grid ClipToBounds="True"
                  x:Name="Animation">
              <Border x:Name="PART_GlowRect"
                         Width="100"
                         HorizontalAlignment="Left"
                         Background="#FF4B8BC2"
                         Margin="-100,0,0,0" />
            </Grid>
          </Border>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
  <Setter Property="Background">
    <Setter.Value>
      <SolidColorBrush Color="#FFF0F0F0" />
    </Setter.Value>
  </Setter>
  <Setter Property="Foreground">
    <Setter.Value>
      <SolidColorBrush Color="#FF4B8BC2" />
    </Setter.Value>
  </Setter>
</Style>

采纳答案by dkozl

I think you should start by reading about ProgressBar Styles and Templates. ProgressBarhas some named parts like PART_Indicatoror PART_Track. They are identified by name in order to be used in correct places. It also has 2 CommonStates: Determinatefor when you want to show real progress and Indeterminatefor when you want to show a progress. You don't have to worry about overlays. If you name parts of the template correctly it will be done automatically for you.

我认为您应该首先阅读ProgressBar Styles and TemplatesProgressBar有一些命名部分,如PART_IndicatorPART_Track。它们按名称标识,以便在正确的位置使用。它还具有 2 CommonStatesDeterminate当您想显示实际进度时以及Indeterminate何时要显示进度时。您不必担心叠加。如果您正确命名模板的各个部分,它将自动为您完成。