0

所以我试图通过悬停放大图像,所以我在网上查看了一些示例,它可以用于单个图像,但是我希望它可以处理多个图像,这些是我在网上找到的代码那就是使用单个图像(感谢 Jeff Yates 提供的解决方案)

xml:

        <Grid.Resources>
            <Storyboard x:Name="growStoryboard">
                <DoubleAnimation
                    Storyboard.TargetProperty="Width"
                    Storyboard.TargetName="testButton"
                    Duration="0:0:0.1"
                    By="20">
                </DoubleAnimation>
                <DoubleAnimation
                    Storyboard.TargetProperty="Height"
                    Storyboard.TargetName="testButton"
                    Duration="0:0:1"
                    By="-20">
                </DoubleAnimation>
            </Storyboard>
            <Storyboard x:Name="shrinkStoryboard">
                <DoubleAnimation
                    Storyboard.TargetProperty="Width"
                    Storyboard.TargetName="testButton"
                    Duration="0:0:1"
                    By="-20">
                </DoubleAnimation>
                <DoubleAnimation
                    Storyboard.TargetProperty="Height"
                    Storyboard.TargetName="testButton"
                    Duration="0:0:0.1"
                    By="20">
                </DoubleAnimation>
            </Storyboard>
        </Grid.Resources>
        <Button x:Name="testButton" Content="Test" Grid.Column="1" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" VerticalAlignment="Center" HorizontalAlignment="Center" Width="50" Height="50">
        </Button>
    </Grid>
</UserControl>

代码隐藏:

public partial class Page : UserControl
{
    public Page()
    {
        InitializeComponent();
    }

    private void Button_MouseEnter(object sender, MouseEventArgs e)
    {
        this.shrinkStoryboard.SkipToFill();
        this.growStoryboard.Begin();
    }

    private void Button_MouseLeave(object sender, MouseEventArgs e)
    {
        this.growStoryboard.SkipToFill();
        this.shrinkStoryboard.Begin();
    }
}
4

1 回答 1

1

我会在 ScaleTransform 上使用动画来做到这一点:

    <Storyboard x:Key="MouseOverRectangleStoryBoard">
        <DoubleAnimation BeginTime="0:0:0" Duration="0:0:0.1" From="1" To="1.1"
                         Storyboard.TargetProperty="(Rectangle.RenderTransform).(ScaleTransform.ScaleX)">
        </DoubleAnimation>
        <DoubleAnimation BeginTime="0:0:0" Duration="0:0:0.1" From="1" To="1.1"
                         Storyboard.TargetProperty="(Rectangle.RenderTransform).(ScaleTransform.ScaleY)">
        </DoubleAnimation>
    </Storyboard>

    <Storyboard x:Key="MouseLeaveRectangleStoryBoard">
        <DoubleAnimation BeginTime="0:0:0" Duration="0:0:0.1" From="1.1" To="1"
                         Storyboard.TargetProperty="(Rectangle.RenderTransform).(ScaleTransform.ScaleX)">
        </DoubleAnimation>
        <DoubleAnimation BeginTime="0:0:0" Duration="0:0:0.1" From="1.1" To="1"
                         Storyboard.TargetProperty="(Rectangle.RenderTransform).(ScaleTransform.ScaleY)">
        </DoubleAnimation>
    </Storyboard>

该代码适用于矩形,但完全相同。

于 2011-05-23T06:09:16.423 回答