自定义用户控件按钮的背景颜色在ShowDialog上更改

时间:2019-07-16 07:38:48

标签: c# wpf xaml

我正在使用WPF在.NET 4.5.2中编写自定义用户控件库。 我的自定义按钮的行为非常奇怪,而现在我真的很绝望。首先,这是项目GitHub

问题
这是带有自定义Button的测试项目的MainWindow

MainWindow

点击事件代码

const oldArr = [{
  "idSport": "102",
  "strSport": "Soccer",
  "strSportThumb": "https://www.thesportsdb.com/images/sports/soccer.jpg",
  "strSportDescription": ""
}]

console.log(oldArr); // console log to show all sports arrays

const newArr = oldArr.map(({idSport:id, ...rest}) => ({ id, ...rest }))
console.log(newArr)

XAML代码

Window1 win1 = new Window1();
win1.ShowDialog();

没什么特别的。现在,单击“按钮”,第二个窗口照常打开。

Second Window

XAML代码

<Grid>
    <Cosmos:CButton x:Name="B1" 
                    C_BackgroundTheme="HKS41" 
                    HorizontalAlignment="Left" 
                    Margin="62,80,0,0" 
                    VerticalAlignment="Top" 
                    Width="117" 
                    Content="ShowDialog" 
                    Click="CButton_Click"/>
</Grid>

ShowDialog从中退出的按钮将其Backgroundcolor更改为XAML代码中的最后一个按钮之一。在这种情况下,较浅的蓝色(HSK44),而不是红色。

代码

这是“自定义”按钮的CS代码:

<Grid>
    <Cosmos:CButton C_BackgroundTheme="Red" HorizontalAlignment="Left" Margin="83,100,0,0" VerticalAlignment="Top" Width="75" Content="Test 2"/>
    <Cosmos:CButton C_BackgroundTheme="HKS44" HorizontalAlignment="Left" Margin="83,63,0,0" VerticalAlignment="Top" Width="75" Content="Test"/>
</Grid>

这是ResourceDictionary

public class CButton : Button
{
    public static readonly DependencyProperty BackgroundThemeProperty = DependencyProperty.Register("C_BackgroundTheme", typeof(Theme), typeof(CButton), new PropertyMetadata(new PropertyChangedCallback(BackgroundValueChanged)));
    public static readonly DependencyProperty ForegroundThemeProperty = DependencyProperty.Register("C_ForegroundTheme", typeof(Theme), typeof(CButton), new PropertyMetadata(new PropertyChangedCallback(ForegroundValueChanged)));

    public static bool Backgroundchanged = false;

    private static string backcolor;
    private static string Backcolor
    {
        get { return backcolor; }
        set
        {
            backcolor = value;
            Backgroundchanged = true;
        }
    }

    static CButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CButton), new FrameworkPropertyMetadata(typeof(CButton)));
    }

    public Theme C_BackgroundTheme
    {
        get
        {
            return (Theme)GetValue(BackgroundThemeProperty);
        }
        set
        {
            SetValue(BackgroundThemeProperty, value);
        }
    }

    public Theme C_ForegroundTheme
    {
        get
        {
            return (Theme)GetValue(ForegroundThemeProperty);
        }
        set
        {
            SetValue(ForegroundThemeProperty, value);
        }
    }

    private static void BackgroundValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = (CButton)d;

        Backcolor = GetColorString((Theme)e.NewValue);

        control.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom(Backcolor));
        control.OpacityMask = (SolidColorBrush)(new BrushConverter().ConvertFrom(ChangeColorBrightness((Color)ColorConverter.ConvertFromString(Backcolor), (float)-0.3).ToString()));
    }

    private static void ForegroundValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = (CButton)d;
        control.Foreground = GetColorBrush((Theme)e.NewValue);
    }

    public static Color ChangeColorBrightness(Color color, float correctionFactor)
    {
        float red = (float)color.R;
        float green = (float)color.G;
        float blue = (float)color.B;

        if (correctionFactor < 0)
        {
            correctionFactor = 1 + correctionFactor;
            red *= correctionFactor;
            green *= correctionFactor;
            blue *= correctionFactor;
        }
        else
        {
            red = (255 - red) * correctionFactor + red;
            green = (255 - green) * correctionFactor + green;
            blue = (255 - blue) * correctionFactor + blue;
        }

        return Color.FromArgb(color.A, (byte)red, (byte)green, (byte)blue);
    }

    protected override void OnMouseEnter(MouseEventArgs e)
    {
        Backcolor = this.Background.ToString();

        ColorAnimation colorChangeAnimation = new ColorAnimation
        {
            To = ChangeColorBrightness((Color)ColorConverter.ConvertFromString(Backcolor), (float)0.15),
            Duration = new Duration(new TimeSpan(0, 0, 0, 0, 100))
        };

        PropertyPath colorTargetPath = new PropertyPath("(Background).(SolidColorBrush.Color)");
        Storyboard CellBackgroundChangeStory = new Storyboard();
        Storyboard.SetTarget(colorChangeAnimation, this);
        Storyboard.SetTargetProperty(colorChangeAnimation, colorTargetPath);
        CellBackgroundChangeStory.Children.Add(colorChangeAnimation);
        CellBackgroundChangeStory.Begin();
    }

    protected override void OnMouseLeave(MouseEventArgs e)
    {
        ColorAnimation colorChangeAnimation = new ColorAnimation
        {
            To = (Color)ColorConverter.ConvertFromString(Backcolor),
            Duration = new Duration(new TimeSpan(0, 0, 0, 0, 100))
        };

        PropertyPath colorTargetPath = new PropertyPath("(Background).(SolidColorBrush.Color)");
        Storyboard CellBackgroundChangeStory = new Storyboard();
        Storyboard.SetTarget(colorChangeAnimation, this);
        Storyboard.SetTargetProperty(colorChangeAnimation, colorTargetPath);
        CellBackgroundChangeStory.Children.Add(colorChangeAnimation);
        CellBackgroundChangeStory.Begin();
    }

    protected override void OnClick()
    {
        Thread.Sleep(10);
        base.OnClick();
    }
}

为什么按钮的行为如此?我也很高兴与《守则》相关的任何提示。

0 个答案:

没有答案
相关问题