MVVM进度条EventTrigger

时间:2017-12-07 03:10:43

标签: c# wpf mvvm wpf-animation

这是我在加载页面时为进​​度条设置动画的示例代码。

我的问题是,如何在按钮点击时启动进度条?

<ProgressBar Height="50" Minimum="0" Maximum="100" Value="0" 
            RenderTransformOrigin="0.5,0.5" 
            Margin="-113,0" Background="Transparent" 
            BorderThickness="0" Foreground="DarkCyan">
     <ProgressBar.RenderTransform>
          <TransformGroup>
              <ScaleTransform/>
              <SkewTransform/>
              <RotateTransform Angle="-90"/>
              <TranslateTransform/>
          </TransformGroup>
     </ProgressBar.RenderTransform>
     <ProgressBar.Triggers>
          <EventTrigger RoutedEvent="FrameworkElement.Loaded">
               <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Value" 
                                        From="0" To="{Binding Bar1}" 
                                        Duration="{Binding Duration1}"/>
                     </Storyboard>
                </BeginStoryboard>
          </EventTrigger>
     </ProgressBar.Triggers>
</ProgressBar>

2 个答案:

答案 0 :(得分:3)

步骤:

<ProgressBar />命名,然后移除Triggers

<ProgressBar x:Name="MyProgressBar" Height="50" Minimum="0" Maximum="100" Value="0" 
            RenderTransformOrigin="0.5,0.5" 
            Margin="-113,0" Background="Transparent" 
            BorderThickness="0" Foreground="DarkCyan">
     <ProgressBar.RenderTransform>
          <TransformGroup>
              <ScaleTransform/>
              <SkewTransform/>
              <RotateTransform Angle="-90"/>
              <TranslateTransform/>
          </TransformGroup>
     </ProgressBar.RenderTransform>
</ProgressBar>

添加<Button />TriggersTargetName等于<ProgressBar />名称。

    <Button Content="Press to trigger">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Value" Storyboard.TargetName="MyProgressBar"
                            From="0" To="100" 
                            Duration="0:0:1"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Button.Triggers>
    </Button>

结果:

enter image description here

答案 1 :(得分:1)

执行此操作的方法之一是DataTrigger

<Button x:Name="BtnToClick" Height="30" Width="100" Content="Click" />

<ProgressBar Height="50" Minimum="0" Maximum="100" Value="0" 
        RenderTransformOrigin="0.5,0.5" 
        Margin="0,0" Background="Transparent" 
        BorderThickness="0" Foreground="DarkCyan">
<ProgressBar.RenderTransform>
  <TransformGroup>
    <ScaleTransform/>
    <SkewTransform/>
    <RotateTransform Angle="-90"/>
    <TranslateTransform/>
   </TransformGroup>
  </ProgressBar.RenderTransform>
  <ProgressBar.Style>
    <Style TargetType="ProgressBar">
       <Style.Triggers>
          <DataTrigger Binding="{Binding IsPressed ,ElementName=BtnToClick}" Value="True">
           <DataTrigger.EnterActions>
              <BeginStoryboard>
                 <Storyboard>
                   <DoubleAnimation Storyboard.TargetProperty="Value" 
                                    From="0" To="100" 
                                    Duration="0:0:0:1"/>
                  </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
           </DataTrigger>
       </Style.Triggers>
      </Style>
    </ProgressBar.Style>
 </ProgressBar>