2012-01-09 99 views
4

我见过一个我找不到的示例WPF程序。在这个例子中,当我点击一个按钮时,另一个按钮开始增长并缩小。这意味着我可以用这个形式做其他事情。我该怎么做呢?如何在WPF中动画控件?

回答

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/