如何在WPF中为控件添加动画效果?

6

我看过一份WPF示例程序,但现在无法找到。在这个示例中,当我点击一个按钮时,另一个按钮开始变大和缩小。同时,我可以在窗体上进行其他操作。我该如何做到这一点?

2个回答

10

以下是一个非常简单的示例,当单击按钮时,按钮的高度\宽度会增加,并在鼠标离开控件时缩小回来。在WPF中,动画是通过使用StoryBoards完成的。 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/zh-cn/library/ms742868.aspx

http://windowsclient.net/learn/


1

你可以使用Storyboard在WPF中使控件动画化。

请查看MSDN上的动画概述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接