如何在WPF中设置动画控制?

时间:2012-01-09 13:59:11

标签: c# wpf animation controls

我见过一个我找不到的WPF程序示例。在此示例中,当我单击按钮时,另一个按钮开始增长和缩小。意思是我可以用表格做其他事情。我该怎么做?

2 个答案:

答案 0 :(得分:9)

下面你会看到一个非常简单的例子,当按钮被点击时按钮高度\宽度增加,当鼠标离开控件时缩小回来。 WPF中的动画是使用StoryBoards完成的。故事板通常位于EventTriggers中,可以保存在控件,窗口,页面或应用程序的资源中。以下是样本和一些资源:

<Window x:Class="WPFFeatureSample_Application.AnimationWindowSample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AnimationWindowSample" Height="300" Width="300">
<Grid>
    <Button Content="Sample" Width="50" Height="50">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <BeginStoryboard>
                <Storyboard>
                        <DoubleAnimation To="200" Storyboard.TargetProperty="Width"></DoubleAnimation>
                        <DoubleAnimation To="200" Storyboard.TargetProperty="Height"></DoubleAnimation>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
            <EventTrigger RoutedEvent="MouseLeave">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation To="50" Storyboard.TargetProperty="Width"></DoubleAnimation>
                        <DoubleAnimation To="50" Storyboard.TargetProperty="Height"></DoubleAnimation>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>
</Grid>

参考文献:

http://msdn.microsoft.com/en-us/library/ms742868.aspx

http://windowsclient.net/learn/

答案 1 :(得分:1)

您可以使用故事板为WPF中的控件设置动画。

查看MSDN上的Animation Overview

相关问题