WPF之 使用Storyboard故事板做动画效果

**


通过Storyboard实现位移和透明度变化动画效果


第一种方式通过Window.Resources在XAML中写的

**

  <Grid>
        <Border x:Name="bor" Background="LightBlue" Margin="0,100,0,-100" Opacity="1"></Border>
        <Button x:Name="btn" Width="100" Height="40" ></Button>
    </Grid>
 <Window.Resources>
        <Storyboard x:Key="sb">
            <ThicknessAnimation Duration="0:0:1" From="0,100,0,-100" To="0,0,0,0" Storyboard.TargetProperty="Margin" Storyboard.TargetName="bor" />
            <DoubleAnimation Duration="0:0:1" From="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="bor"/>
        </Storyboard>
    </Window.Resources>

怎么触发呢!!!

 <Window.Triggers>
        <EventTrigger RoutedEvent="Button.Click" SourceName="btn">
            <BeginStoryboard Storyboard="{StaticResource sb}">
                
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>

第二种方式 通过Button的Click事件触发

 ThicknessAnimation thicknessAnimation = new ThicknessAnimation();
            thicknessAnimation.Duration = new TimeSpan(0, 0, 0, 1);
            thicknessAnimation.From = new Thickness(0, 100, 0, -100);
            thicknessAnimation.To = new Thickness(0, 0, 0, 0);
            //this.bor.BeginAnimation(MarginProperty, thicknessAnimation);

            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.Duration = new TimeSpan(0, 0, 0, 1);
            doubleAnimation.From = 0;
            doubleAnimation.To = 1;
            //this.bor.BeginAnimation(OpacityProperty, doubleAnimation);

            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add(thicknessAnimation);
            storyboard.Children.Add(doubleAnimation);

            Storyboard.SetTarget(thicknessAnimation, this.bor);
            Storyboard.SetTargetProperty(thicknessAnimation, new PropertyPath("Margin"));
            Storyboard.SetTarget(doubleAnimation, this.bor);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity"));
            storyboard.Begin();

效果图
WPF之 使用Storyboard故事板做动画效果_第1张图片

你可能感兴趣的:(WPF,wpf)