从数据库(WPF)更改背景颜色

本文关键字:背景 颜色 数据库 WPF | 更新日期: 2023-09-27 18:04:01

感谢你花时间看我的问题,我看了几个问题,没有找到任何关于这个的,所以我想做的是:

1 -我想有一个表:Colors_Table为例,然后保存所有的十六进制的颜色。

2-设置十六进制颜色,将用于我的客户端,如Properties_Table,然后有一个行MainBackGroundColor,这是我将使用我的背景的颜色。

我不知道我是否说清楚了,基本上我想要的是能够从数据库中设置背景色

从数据库(WPF)更改背景颜色

你的问题很清楚。要做到这一点,你可以添加一个背景属性到你的窗口,像这样…

<Window.Background>
    <SolidColorBrush Color="{Binding MyBackgroundColor}"/>
</Window.Background>

注意画笔的颜色是绑定到属性的,而不是硬编码的值。

然后在你的代码后面,你可以使用这个…

public partial class Window1 : Window, INotifyPropertyChanged
{
    public Window1()
    {
        InitializeComponent();
        DataContext = this;
        // set color here
        MyBackgroundColor = Colors.Red;
    }
    private Color _myBackgroundColor;
    public Color MyBackgroundColor
    {
        [DebuggerStepThrough]
        get { return _myBackgroundColor; }
        [DebuggerStepThrough]
        set
        {
            if (value != _myBackgroundColor)
            {
                _myBackgroundColor = value;
                OnPropertyChanged("MyBackgroundColor");
            }
        }
    }
    #region INotifyPropertyChanged Implementation
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string name)
    {
        var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
    #endregion
}

在本例中,背景为红色。如果你有一个十六进制格式的字符串,比如"#ffaabbcc",你可以使用这个转换…

        MyBackgroundColor =(Color) new ColorConverter().ConvertFrom(null, null, "#ffaabbcc");

…并得到你想要的结果。

指出:

  • '#'应该是字符串的第一个字符。

    前两个字节是alpha通道