【发布时间】:2015-11-06 06:42:31
【问题描述】:
我在 WPF 中有一个 DataGrid,我想将行的 BackgrounColor 绑定到我作为 ItemsSource 提供给数据网格的集合中各个项目的属性。
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));
}
}
我正在尝试将数据网格行的 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>
但它没有设置背景颜色。我不知道我做错了什么。
【问题讨论】: