SolidColorBrush bgColor; public ModernBTN() { InitializeComponent(); this.Loaded += delegate (object sender, RoutedEventArgs e) { bgColor = (SolidColorBrush)Background; MouseEnter += EnterAnim; MouseLeave += LeaveAnim; }; } private void EnterAnim(object sender, MouseEventArgs e) { 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); } private void LeaveAnim(object sender, MouseEventArgs e) { DoubleAnimation anim = new DoubleAnimation(myBtn.Width, 0, TimeSpan.FromMilliseconds(200)); indicatorBtn.BeginAnimation(WidthProperty, anim); ColorAnimation animC = new ColorAnimation(Color.FromArgb(bgColor.Color.A, bgColor.Color.R, bgColor.Color.G, bgColor.Color.B), TimeSpan.FromMilliseconds(200)); myBtn.Background.BeginAnimation(SolidColorBrush.ColorProperty, animC); }
S’il vous plaît dites-moi pourquoi bgColor change en couleur BGHover si je mets les valeurs bgColor uniquement dans cet arrière-plan.Chargé et = (SolidColorBrush).
ModernBtn.xaml code xaml de mon bouton
bgColor = (SolidColorBrush) Arrière-plan;
Comme SolidColorBrush
est un type de référence, bgColor
et Background
référenceront le même object après la ligne ci-dessus. Ainsi, lorsque des modifications sont apscopes à Background
(comme vous le faites avec l’animation), ces modifications seront reflétées dans bgColor
.
Un moyen facile de résoudre ce problème peut être de changer bgColor
en 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); }