一、动画定义可以在xaml代码中定义。
MainWindow.xaml文件代码(按键盘→键可以看到右侧被挡住的代码)
<Window x:Class="WpfApp3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary>
<Style TargetType="Button" x:Key="ButtonStyle">
<Style.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation To="150" Duration="0:0:1" Storyboard.TargetProperty="Width"/>
<DoubleAnimation To="150" Duration="0:0:1" Storyboard.TargetProperty="Height"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:1" Storyboard.TargetProperty="Width"/>
<DoubleAnimation Duration="0:0:1" Storyboard.TargetProperty="Height"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="201,120,0,0" VerticalAlignment="Top" Width="75" Height="75" Style="{StaticResource ButtonStyle}"/>
</Grid>
</Window>
代码效果如下:
代码效果
网友评论