2013-04-18 86 views
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(); 
} 
+0

要確定當前列標題 - 您是否嘗試檢查轉換器中當前的DataColumn的ColumnName? –

+0

@SandraWalters:謝謝你的想法:我已經嘗試過,但CurrentColumn是空:( –

回答

0

我終於foubnd一個解決方案,使用Multibinding:

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

     } 
相關問題