Datagrid行背景颜色绑定到数据源集合中单个项目中的属性

时间:2015-08-13 10:37:29

标签: c# wpf xaml datagrid

我在WPF中有一个DataGrid,我希望将行的BackgrounColor绑定到属​​性集合中的各个项目,我将其作为ItemsSource提供给datagrid。

   public class Log: INotifyPropertyChanged
{
    private string _timestamp;
    private string _threadName;
    private string _userName;
    private string _message;
    private Brush _backgroundColor;

    public string Timestamp
    {
        get { return _timestamp; }
        set
        {
            if (_timestamp == value) return;
            _timestamp = value;
            NotifyPropertyChanged("Timestamp");
        }
    }
    public string ThreadName
    {
        get { return _threadName; }
        set
        {
            if (_threadName == value) return;
            _threadName = value;
            NotifyPropertyChanged("ThreadName");
        }
    }
    public string UserName
    {
        get { return _userName; }
        set
        {
            if (_userName == value) return;
            _userName = value;
            NotifyPropertyChanged("UserName");
        }
    }
    public string Message
    {
        get { return _message; }
        set
        {
            if (_message == value) return;
            _message = value;
            NotifyPropertyChanged("Message");
        }
    }

    public string BackgroundColor
    {
        get { return _backgroundColor; }
        set
        {
            if (_backgroundColor== value) return;
            _backgroundColor = value;
            NotifyPropertyChanged("BackgroundColor");
        }
    }


    public bool IsCustomLog = false;
    public string HighlightColor = null;

    public event PropertyChangedEventHandler PropertyChanged;

    //Constructor
    public Log(string timestamp, string threadName, string userName, string message, Brush backgroundColor)
    {
        UserName = userName;
        Timestamp = timestamp;
        ThreadName = threadName;
        Message = message;
        BackgroundColor = backgroundColor;
    }

    //Methods
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

我正在尝试将datagrid行的BackgroundColor绑定到Log类中的BackgroundColor属性。

我尝试像这样绑定它:

            <DataGrid.RowStyle>
            <Style TargetType="{x:Type DataGridRow}">
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="FontFamily" Value="Arial"/>
                <Setter Property="BorderThickness" Value="0,0,2,2"/>
                <Setter Property="Background" Value="{Binding BackgroundColor}"/>
                <Setter Property="BorderBrush" Value="#CCCCCC"/>
            </Style>
        </DataGrid.RowStyle>

但它没有设置背景颜色。我不知道我做错了什么。

1 个答案:

答案 0 :(得分:1)

由于_backgroundColorBrushBackgroundColorstring,我不确定您的代码是否是拼写错误:

public string BackgroundColor
{
    get { return _backgroundColor; }
    set
    {
        if (_backgroundColor== value) return;
        _backgroundColor = value;
        NotifyPropertyChanged("BackgroundColor");
    }
}

我建议您自属性名称的“颜色”后缀后BackgroundColor Color,并更改XAML中的'Setter',如下所示:

<Setter Property="Background">
    <SolidColorBrush Color="{Binding BackgroundColor}"/>
</Setter>
相关问题