SolidColorBrush bgColor;
public ModernBTN() {
InitializeComponent();
this.Loaded += delegate (object sender, RoutedEventArgs e) {
bgColor = (SolidColorBrush)Bac
bgColor = (SolidColorBrush)Background;
Because SolidColorBrush
is a reference type, bgColor
and Background
will reference the same object after the above line. So, when changes are made to Background
(as you do with the animation), this changes will be reflected in bgColor
.
An easy way to solve this may be to changebgColor
to type Color
:
Color bgColor;
public MainWindow()
{
InitializeComponent();
this.Loaded += delegate (object sender, RoutedEventArgs e) {
bgColor = ((SolidColorBrush)Background).Color;
MouseEnter += EnterAnim;
MouseLeave += LeaveAnim;
};
}
private void EnterAnim(object sender, MouseEventArgs e)
{
ColorAnimation animC = new ColorAnimation(BGHover, TimeSpan.FromMilliseconds(200));
myBtn.Background.BeginAnimation(SolidColorBrush.ColorProperty, animC);
}
private void LeaveAnim(object sender, MouseEventArgs e)
{
ColorAnimation animC = new ColorAnimation(bgColor, TimeSpan.FromMilliseconds(200));
myBtn.Background.BeginAnimation(SolidColorBrush.ColorProperty, animC);
}