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

C# 基于属性的Listview项背景色(WPF/MVVM),c#,wpf,C#,Wpf,我想在ListView中显示标题和颜色(参见图片-用相应的颜色替换14474460) 颜色将由颜色选择器拾取(参见“橙色”)。所以一切正常,SQLite数据库更新了,数字也相应地改变了,但我不能改变背景 我的XAML <GridViewColumn Header="Color"> <GridViewColumn.CellTemplate> <DataTemplate>

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

颜色将由颜色选择器拾取(参见“橙色”)。所以一切正常,SQLite数据库更新了,数字也相应地改变了,但我不能改变背景

我的XAML

            <GridViewColumn Header="Color">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Color}"  Background="{Binding ColorBackground}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
我的背景色属性(绑定到textblock-我想我需要一个笔刷,对吗?)

我的类型实体

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));
        }
    }
}
我的助手messagebox(更新方法之后)返回ColorBackground的十六进制代码,所以我想这不是问题所在。 有人能帮忙吗

更新 ColorBackground属性(ViewModel文件)

颜色属性(ViewModel文件)


所以你改变颜色。你不改变颜色背景;你什么都不做。任何绑定到ColorBackground的人怎么知道应该设置ColorBackground以强制ColorBackground更新自身,然后重新获得ColorBackground?UI中的任何代码怎么可能知道您希望它做所有这些事情?方法如下:使ColorBackground成为普通的get/set属性,而不是setter中的聪明东西。然后让颜色属性的setter在颜色更改时设置它:
ColorBackground=newsolidcolorbush(Color)。LeaveTypeEntity用于什么?它与其他类具有相同的属性,尽管它们的定义略有不同。你确定你知道这些UI属性绑定到两个类中的哪一个吗?我使用LeaveTypeEntity进行数据库更新(它工作得很好——尽管我完成后必须重构整个代码。这是我的第一个项目,所以可能会有一些奇怪的代码)。照你说的做,当选择一种新颜色时,我从ColorBackground得到一个十六进制代码作为回报(MessageBox),所以至少十六进制代码在这里,但文本块仍然是白色的,所以看起来
LeaveTypeEntity
是ListView中的项。这是您在DataTemplate中使用TextBlock.Background绑定到的BackgroundBrush属性,这是您在DataTemplate中将TextBlock.Text绑定到的颜色属性。我不知道如何或是否在ListView项中设置
LeaveTypeEntity.Color
,但如果您以某种方式这样做,然后您的
LeaveTypeEntity.Color
属性的setter需要设置
LeaveTypeEntity.BackgroundBrush
,就像ViewModel类中相应的属性一样。如果
LeaveTypeEntity
ListView
的数据模型(?)您需要确保
ColorBackground
属性有一个值。您发布了三个不同版本的
颜色
颜色背景
属性。我认为你应该澄清和改进你的问题。
public Brush ColorBackground
{
    get { return colorbackground; }
    set
    {
        colorbackground = new SolidColorBrush(Color);
        NotifyPropertyChanged("ColorBackground");
    }
}
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));
        }
    }
}
public Brush ColorBackground { get; set; }
private Color color;
public Color Color
{
    get { return color; }
    set
    {
        color = value;
        ColorBackground = new SolidColorBrush(Color);
        NotifyPropertyChanged("Color");
    }
}