美文网首页
WPF例8-鼠标放在按钮上,按钮自动变大

WPF例8-鼠标放在按钮上,按钮自动变大

作者: quchangTJU | 来源:发表于2020-01-28 15:16 被阅读0次

一、动画定义可以在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>

代码效果如下:


代码效果

相关文章

网友评论

      本文标题:WPF例8-鼠标放在按钮上,按钮自动变大

      本文链接:https://www.haomeiwen.com/subject/umzpthtx.html