我想制作一个具有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相同。