2016-09-27 60 views
-2

我正在获取绑定到WPF数据网格的数据表中的动态列(因为列动态生成,因此没有模型)。这些是自动生成的列。我需要更改单元格的颜色数据网格是基于条件的。有一列(Progress),每列有一个逗号分开的值。这个数字将决定column1,column2,column3的单元格的颜色。我需要一种方法来设置单个单元格的样式颜色,通过使用来自特定列的列(Progress)的值。DataGrid使用动态生成列的单元格样式

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

+0

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

回答

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;} 
} 
相关问题