【问题标题】:DataGrid Cell styling with Dynamic generating column具有动态生成列的 DataGrid 单元格样式
【发布时间】:2016-09-27 12:52:34
【问题描述】:

我在绑定到 WPF 数据网格的数据表中获取动态列(因为列是动态生成的,所以没有模型)。这些是自动生成的列。我需要根据条件更改数据网格的单元格颜色。那里是列(进度),每列都有一个逗号分隔值。这些数字将决定 column1、column2、column3 的单元格的颜色。我需要一种方法来使用列(进度)中的值来设置单个单元格的样式颜色特定列。

| Coulmn1    | Coumn2        | Column3       | Progress         |
| 123        | ABC           | TRUE          | C1=0.5,C2=1,C3=0 |
| 456        | CDF           | TRUE          | C1=1,C2=1,C3=0   |
| 789        | EFG           | TRUE          | C1=0,C2=1,C3=0   |

颜色将是0=Red,0.5=LightRed,1=Green,例如

| Coulmn1              | Coumn2          | Column3       | Progress         |
| 123   (LightRed)     | ABC (Green)     | TRUE    (Red) | C1=0.5,C2=1,C3=0 |

【问题讨论】:

  • DataGridCell 创建一个隐式样式并使用转换器设置它的背景颜色。转换器需要知道行的数据项和单元格所在的列,因此它可能需要是MultiValueConverter。转换器需要将数据项转换为DataGridRow 对象,获取Progress 单元格,并根据列决定返回哪种颜色。或者,您可以为您的 DataGrid 创建一个适当的支持对象并将您的数据解析为这些对象,其中将包含一个您可以绑定到的 Color 属性。我的偏好是#2。

标签: wpf mvvm datagrid


【解决方案1】:

试试这个: 帮手:

  public class ColumnNameAttribute : Attribute
    {
        public string Name { get; set; }
        public TextAlignment TextAlignment { get; set; }
        public string Color { get; set; }
        public bool IsReadOnly { get; set; }
        public double MinWidth { get; set; }
        /// <summary>
        /// Helper to fill column attributes: header name, text alignment, editability and background color
        /// </summary>
        /// <param name="name"></param>
        /// <param name="textAlignment"></param>
        /// <param name="isReadOnly"></param>
        /// <param name="color"></param>
        public ColumnNameAttribute(string name, TextAlignment textAlignment = TextAlignment.Left, bool isReadOnly = true, string color = "White")
        {
            Name = name;
            TextAlignment = textAlignment;
            Color = color;
            IsReadOnly = isReadOnly;
        }

        public ColumnNameAttribute(string name, double minWidth, TextAlignment textAlignment= TextAlignment.Left, bool isReadOnly = true, string color = "White")
        {
            Name = name;
            TextAlignment = textAlignment;
            IsReadOnly = isReadOnly;
            Color = color;
        }
    }

xaml.cs:

datagrid.AutoGeneratingColumn +=  dataGrid_AutoGeneratingColumn;

 private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            var desc = e.PropertyDescriptor as PropertyDescriptor;
            var att = desc.Attributes[typeof(ColumnNameAttribute)] as ColumnNameAttribute;
            if (att != null)
            {
                e.Column.IsReadOnly = att.IsReadOnly;
                e.Column.CellStyle = new Style(typeof(DataGridCell));
 switch (att.Color)
                {
                    case "LightGreen":
                        //BorderBrushProperty looks better
                        e.Column.CellStyle.Setters.Add(new Setter( BackgroundProperty, new SolidColorBrush(Colors.PaleGreen)));
                        e.Column.CellStyle.Setters.Add(new Setter(TextBox.ToolTipProperty, "<Enter value>"));
                        break;
                }
                e.Column.Header = att.Name;
                e.Column.CellStyle.Setters.Add(new Setter(TextBlock.TextAlignmentProperty, att.TextAlignment));
                if (att.MinWidth > 0) e.Column.MinWidth = att.MinWidth;
            }
        }

对象类:

public Example{
 [ColumnName("My Custom Name", TextAlignment.Right, false, "LightGreen")]
   public string Name {get; set;}
}

【讨论】:

    猜你喜欢
    • 2011-05-12
    • 2020-03-27
    • 2011-01-27
    • 2016-04-28
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多