背景颜色变化 [英] Background Color Changes

查看:75
本文介绍了背景颜色变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

请告诉我为什么我将bgColor值设置为bgColor变为BGHover颜色

Please tell me why bgColor changes to BGHover color if I put bgColor values ​​only in this.Loaded and = (SolidColorBrush) Background.

ModernBtn.xaml我的按钮的xml代码

ModernBtn.xaml xaml code of my button

<UserControl x:Class="ModernButton.ModernBTN"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:ModernButton"
         mc:Ignorable="d" 
         d:DesignHeight="100" d:DesignWidth="200" Name="myBtn" Background = '#FF282829'>
<Grid Width="Auto" Height="Auto" Name="mainGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>
    <TextBlock Text="{Binding ElementName=myBtn, Path=BtnText}" FontSize="24" Foreground="#FFC7BBBB" TextAlignment="Center" Grid.Row="0" Name="txtBtn" Padding="{Binding ElementName=myBtn, Path=TextPadding}"></TextBlock>
    <Rectangle Grid.Row="1" Fill="Lime" Height="5" Name="indicatorBtn" Width="0"></Rectangle>
</Grid>

推荐答案


bgColor =(SolidColorBrush)Background;

bgColor = (SolidColorBrush)Background;

因为 SolidColorBrush 是引用类型, bgColor Background 将在上一行之后引用同一对象。因此,当对背景进行更改时(与动画一样),此更改将反映在 bgColor

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.

一种简单的解决方法可能是将 bgColor 更改为 Color

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

这篇关于背景颜色变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆