首页 > 解决方案 > 悬停在 UserControl 中时的动画

问题描述

我想制作一个具有 2 个 Background 和 BGHover 属性的 UserControl。当您将光标悬停在 UserControl 上时,Background 控件将更改为 BGHover(很可能在 ColorAnimation 的帮助下),并且当 MouseLeave Background 更改回原来的那个时。

SolidColorBrush bgColor = (SolidColorBrush)Background;
        this.MouseEnter += delegate (object sender, MouseEventArgs e) {
            bgColor = (SolidColorBrush)Background;
            DoubleAnimation anim = new DoubleAnimation(0, myBtn.Width, TimeSpan.FromMilliseconds(200));
            indicatorBtn.BeginAnimation(WidthProperty, anim);
            ColorAnimation animC = new ColorAnimation(BGHover, TimeSpan.FromMilliseconds(200));
            myBtn.Background.BeginAnimation(SolidColorBrush.ColorProperty, animC);
        };
        this.MouseLeave += delegate (object sender, MouseEventArgs e) {
            DoubleAnimation anim = new DoubleAnimation(myBtn.Width, 0, TimeSpan.FromMilliseconds(200));
            indicatorBtn.BeginAnimation(WidthProperty, anim);
            ColorAnimation animC = new ColorAnimation(BGHover, Color.FromArgb(bgColor.Color.A, bgColor.Color.R, bgColor.Color.G, bgColor.Color.B), TimeSpan.FromMilliseconds(200));
            myBtn.Background.BeginAnimation(SolidColorBrush.ColorProperty, animC);
        };

通过悬停,我将背景更改为 BGHover 颜色。但是使用 MouseLeave,颜色保持与 BGHover 相同。

标签: c#

解决方案


推荐阅读