【问题标题】:Datagrid row background color bind to property in individual item from data source collection数据网格行背景颜色绑定到数据源集合中单个项目的属性
【发布时间】: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>

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

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2011-11-12
      • 1970-01-01
      • 2011-11-04
      • 2018-02-02
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 2012-01-13
      • 2013-09-02
      相关资源
      最近更新 更多