将鼠标悬停在放大多个图像

发布于 2024-11-09 10:14:31 字数 2268 浏览 0 评论 0原文

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

Xaml:

        <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();
    }
}

So I am trying to enlarge an image with hover over, so I looked at a few examples online and it is able to work for a single image, however I would like for it to work with multiple images, these are the codes i found online thats work with a single image(credit to Jeff Yates for providing the solution)

Xaml:

        <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>

Code-behind:

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那片花海 2024-11-16 10:14:31

我会在 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>

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

I would use an animation on ScaleTransform to do this like so:

    <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>

The code is for a rectangle but it's exactly the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文