2012-01-09 82 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/