0
我想爲我的DataGrid ColumnHeader上色,具體取決於綁定到DataGrid的數據表中的行。 但我該怎麼做?我在Datagrid.Row上使用的方法不起作用,因爲顯然沒有可以使用的「Column」屬性。將Datagrid Columnheader Backgroud綁定到da datatable行
我想出了這個,所以我可以在我的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>
代碼Colorconverter背後:
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();
}
要確定當前列標題 - 您是否嘗試檢查轉換器中當前的DataColumn的ColumnName? –
@SandraWalters:謝謝你的想法:我已經嘗試過,但CurrentColumn是空:( –