0

我想根据绑定到 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();
}
4

1 回答 1

0

我终于找到了一个解决方案,使用多重绑定:

XAML:

  <Style TargetType="DataGridColumnHeader">
                    <Setter Property="Background">
                        <Setter.Value>
                            <MultiBinding Converter="{StaticResource ExcelColumnColorConverter}">
                                <Binding RelativeSource="{RelativeSource AncestorType=DataGrid}"></Binding>
                                <Binding RelativeSource="{RelativeSource self}" Path="Column"></Binding>
                            </MultiBinding>
                        </Setter.Value>
                    </Setter>
                </Style>

转换器:

 public class ExcelColumnColorConverter : IMultiValueConverter
    {
   public object Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!(value[1] == null))
            {
                var dgrid = (DataGrid)value[0];
                var colHeader = ((DataGridTextColumn)value[1]).Header.ToString();

                var dView = (System.Data.DataView)dgrid.ItemsSource;
                var table = dView.Table;

                var rowstateObj = table.Rows[table.Rows.Count - 1][colHeader];
                Enums.RowState colorValue = (Enums.RowState)Enum.Parse(typeof(Enums.RowState), rowstateObj.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;
                }
            }
            else
            {
                return null;
            }

        }
于 2013-04-19T08:53:27.033 回答