基于属性的Listview项背景色(WPF / MVVM)

时间:2019-10-21 18:19:31

标签: c# wpf

我想在ListView enter image description here中显示标题和颜色(参见图片-用相应的颜色替换14474460)

颜色将由颜色选择器选择(请参见“橙色红色”)。这样一切正常,SQLite数据库已更新,数字相应更改,但我无法更改背景。

我的XAML

            <GridViewColumn Header="Color">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Color}"  Background="{Binding ColorBackground}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>

我的Color属性(绑定到ColorPicker-将颜色存储为System.Windows.Media.Color)

public Color Color
{
    get { return color; }
    set
    {
        color = value;
        NotifyPropertyChanged("Color");
    }
}

我的背景颜色属性(绑定到文本块-猜猜我需要在这里刷,对吧?)

public Brush ColorBackground
{
    get { return colorbackground; }
    set
    {
        colorbackground = new SolidColorBrush(Color);
        NotifyPropertyChanged("ColorBackground");
    }
}

我的LeaveType实体

using Dapper.Contrib.Extensions;
using System.ComponentModel;
using System.Windows.Media;

namespace Calendar.Database.Entities
{
    [Dapper.Contrib.Extensions.Table("LeaveTypes")]
    public class LeaveTypeEntity : Entity, INotifyPropertyChanged
    {

        private long color;
        public long Color
        {
            get
            {
                return color;
            }
            set
            {
                color = value;
                NotifyPropertyChanged("Color");
            }
        }

        private string type;
        public string Type
        {
            get
            {
                return type;
            }
            set
            {
                type = value;
                NotifyPropertyChanged("Type");
            }
        }

        private Brush colorbackground;
        [Computed]
        public Brush ColorBackground
        {
            get { return colorbackground; }
            set
            {
                colorbackground = value;
                NotifyPropertyChanged("ColorBackground");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

我的帮助程序消息框(在update方法之后)返回ColorBackground的十六进制代码,所以我想这不是问题。 有人可以帮忙吗?

更新 ColorBackground属性(ViewModel文件)

public Brush ColorBackground { get; set; }

颜色属性(ViewModel文件)

private Color color;
public Color Color
{
    get { return color; }
    set
    {
        color = value;
        ColorBackground = new SolidColorBrush(Color);
        NotifyPropertyChanged("Color");
    }
}

0 个答案:

没有答案