2011-12-15 31 views
0

我在WPF項目窗口中有WPF DataGrid。我用DataTable填充了網格,並自動生成了列(不幸是必需的),並且需要根據某些其他因素更改列的標題顏色。在運行時自動生成的列中更改WPF數據網格中列標題的顏色

我有一個需要突出顯示的列名稱列表,並且很容易能夠根據這個找出它們的索引(因爲我自己在DataGrid中生成了它們)。

但是,我似乎無法得到列標題來改變顏色,這必須在代碼中完成,因爲我不知道在設計時哪些列將需要突出顯示。我在頭上已經有了一些模板......不知道這是否「覆蓋」了我想要做的事情。

網:

<DataGrid FrozenColumnCount="1" AutoGenerateColumns="True" Grid.Row="1" 
    AlternationCount="2" HeadersVisibility="Column" Name="dgSkillsMatrix" 
    Margin="0,0,2,1" HorizontalGridLinesBrush="White" VerticalGridLinesBrush="White" 
    AlternatingRowBackground="#FFD0D0EB" RowBackground="#FFECECF5" FontSize="10.5" 
    Grid.ColumnSpan="1" CellStyle="{StaticResource CellHighlighterStyle}" 
    ColumnHeaderStyle="{StaticResource dataGridColumnHeader}" /> 

頁眉模板/風格:

<DataTemplate x:Key="RotateHeaderTemplate" > 
    <TextBlock Text="{Binding}" Foreground="Blue" > 
     <TextBlock.LayoutTransform> 
      <RotateTransform Angle="-90" /> 
     </TextBlock.LayoutTransform> 
    </TextBlock> 
</DataTemplate> 

這是我迄今試圖讓列標題改變(呼籲Window_Activated事件,因爲這是在實際構建Grid/WPF樹時在構造函數後調用):

Style newStyle = new System.Windows.Style() 
{ 
    TargetType = typeof(DataGridColumn) 
}; 

// SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#F70F49")) 
newStyle.Setters.Add(new Setter(DataGridColumn.HeaderStringFormatProperty, new SolidColorBrush(Colors.Red))); 
this.dgSkillsMatrix.Columns[4].HeaderStyle = newStyle; 
+0

爲什麼是你的風格設置`HeaderStringFormat`爲`Colors.Red `? – Rachel 2011-12-15 15:25:28

+0

我當時只是試圖突出顯示文本,這將是可以接受的,但我寧願突出顯示整個「標題」單元格。這不起作用,但它仍然運行,並沒有給出任何WPF例外。 – 2011-12-15 15:48:03

回答

5

這不會像這樣工作。這有很多原因。

  1. 你正在刷DataGridColumn.HeaderStringFormatProperty >>這不起作用。
  2. 無論如何,這將不會被轉移到列標題。

要能夠做到這一點,您需要了解DataGridColumnHeader的dataContext默認情況下不會設置爲相應的列。你必須手動設置它。

看看這篇文章:https://stackoverflow.com/a/5249223/479384

現在就你而言,我會做同樣的方式的東西在上面mentionned鏈接:

添加你需要的依賴屬性在你的DataGridColumn的類:

private static readonly DependencyProperty ColumnHeaderTextProperty = DependencyProperty.Register("ColumnHeader", typeof(string), typeof(MyDataGridColumn)); 
public string ColumnHeaderText 
{ 
    get { return (string)(GetValue(ColumnHeaderTextProperty)); } 
    set { SetValue(ColumnHeaderTextProperty, value); } 
} 

private static readonly DependencyProperty ColumnHeaderBackgroundProperty = DependencyProperty.Register("ColumnHeader", typeof(Brush), typeof(MyDataGridColumn)); 
public Brush ColumnHeaderBackground 
{ 
    get { return (Brush)(GetValue(ColumnHeaderBackgroundProperty)); } 
    set { SetValue(ColumnHeaderBackgroundProperty , value); } 
} 

然後設置的DataContext在列的構造是這樣的:

public MyDataGridColumn() 
{ 
    Header = this; 
    ColumnHeaderText = "My header text"; 
} 

(那你以前有Header = "my header text";代替)

最後,更新您的頭部模板:

<DataGrid.ColumnHeaderStyle> 
    <Style TargetType="{x:Type DataGridColumnHeader}"> 
     <Setter Property="ContentTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <TextBlock Text="{Binding ColumnHeaderText}" Foreground="Blue" Background="{Binding ColumnHeaderBackground}"> 
         <TextBlock.LayoutTransform> 
          <RotateTransform Angle="-90" /> 
         </TextBlock.LayoutTransform> 
        </TextBlock> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</DataGrid.ColumnHeaderStyle> 

然後,在後面的代碼,當你想改變標題的背景,只是這樣做:

((MyDataGridColumn)(this.dgSkillsMatrix.Columns[4])).ColumnHeaderBackground = Brushes.Red; 

編輯:如果你沒有爲你列一類特殊的,你應該能夠做到這一點的Wi TH附加屬性,你可以隨時把我在構造函數中其他地方這樣寫的代碼:

myColumn.Header = myColumn; 
myColumn.SetValue(columnHelper.ColumnHeaderTextProperty, "my Header Text"); 
myColumn.SetValue(columnHelper.ColumnHeaderBackgroundProperty, Brushes.Red); 

編輯爲hashlock ^^

相關問題