使用动态生成列的DataGrid单元样式

时间:2016-09-27 12:52:35

标签: wpf mvvm datagrid

我在数据表中获取动态列(因为列生成动态,因此没有模型)绑定到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 =红色,0.5 = LightRed,1 =绿色 例如 | Coulmn1 | Coumn2 | Column3 | Progress | | 123 (LightRed) | ABC (Green) | TRUE (Red) | C1=0.5,C2=1,C3=0 |

1 个答案:

答案 0 :(得分:0)

试试这个: 助手:

  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;}
}