我想根据绑定到 DataGrid 的数据表中的一行来为我的 DataGrid ColumnHeader 着色。但是我该怎么做呢?我在 Datagrid.Row 上使用的方法不起作用,因为显然没有我可以使用的“列”属性。
我想出了这个,所以我可以在我的 Colorconverter 中使用整个 DataGrid,但是我无法找到一种方法来确定当前呈现的是哪个 Columnheader。
XAML:
<DataGrid x:Name="excelDataTable_ExcelData">
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Converter={StaticResource ExcelColumnColorConverter}}"></Setter>
</Style>
</DataGrid>
颜色转换器背后的代码:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var col = (System.Data.DataColumn)value;
var colStateObj = col.Table.Rows[col.Table.Rows.Count-1][col.ColumnName];
Enums.RowState colorValue = (Enums.RowState)Enum.Parse(typeof(Enums.RowState), colStateObj.ToString());
switch (colorValue)
{
case Enums.RowState.HeaderRow:
return Brushes.Gainsboro;
case Enums.RowState.isIncluded:
return Brushes.LightGreen;
case Enums.RowState.NotIncluded:
return Brushes.LightSalmon;
default:
return Brushes.Azure;
}
}
/// <summary>
/// Converts the value of the hidden Color row to a color on the Data table.
/// </summary>
/// <param name="value">The value</param>
/// <param name="targetType">The type of the binding target.</param>
/// <param name="parameter">The converter parameter.</param>
/// <param name="culture">The culture</param>
/// <returns>
/// EThe converted value or NULL
/// </returns>
/// <exception cref="System.NotImplementedException">Not implemented because it is not needed</exception>
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}