2013-10-02 65 views
19

我需要將WPF DataGrid Column Header文本對齊中心。我創建了一個樣式,並使用下面的HeaderStyle屬性進行附加。將DataGrid列標題對齊中心

風格

<Window.Resources> 
    <Style x:Key="CenterGridHeaderStyle" TargetType="DataGridColumnHeader"> 
     <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    </Style> 
</Window.Resources> 

<DataGridTextColumn 
    Binding="{Binding Path=Name}" Header="Name" IsReadOnly="True" 
    HeaderStyle="{DynamicResource CenterGridHeaderStyle}"/> 

但這不是對齊列標題文本到中心。我怎樣才能做到這一點 ?

+0

你可以找到答案: http://stackoverflow.com/questions/7943729/how-to-set-style-for-datagrid-header-in-wpf –

+0

嘗試這樣的: HTTP ://stackoverflow.com/questions/7943729/how-to-set-style-for-datagrid-header-in-wpf –

回答

43

入住這

<DataGridTextColumn Header="Nombre" 
          Binding="{Binding Nombre}"> 
<DataGridTextColumn.HeaderStyle> 
    <Style TargetType="DataGridColumnHeader"> 
    <Setter Property="HorizontalContentAlignment" 
       Value="Center" /> 
    </Style> 
</DataGridTextColumn.HeaderStyle> 
+0

將不勝感激任何評論爲什麼你的解決方案不起作用,而不是@Bishan的解決方案。 – Tatranskymedved

0

試試這個

<DataGridTextColumn.CellStyle> 
    <Style> 
    <Setter Property="HorizontalAlignment" Value="Center" /> 
    </Style> 
</DataGridTextColumn.CellStyle> 
+0

http://stackoverflow.com/a/18179080/2382032 –

0

有在AutoGeneratingColumn編程做一個迴應:

private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
    { 
       e.Column.HeaderStyle = new Style(typeof(DataGridColumnHeader)); 
       e.Column.HeaderStyle.Setters.Add(new Setter(HorizontalContentAlignmentProperty, HorizontalAlignment.Center)); 

    } 

提示,使用屬性:

public class ColumnNameAttribute : Attribute 
{ 
    public HorizontalAlignment Alignment { get; set;} 
    public ColumnNameAttribute(HorizontalAlignment alignment){ 
     Alignment = alignment; 
} 

public class Example(){ 
    [ColumnName(HorizontalAlignment.Center)] 
    public string Column {get; set;} 
} 

private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
{ 
    var desc = e.PropertyDescriptor as PropertyDescriptor; 
    var att = desc.Attributes[typeof(ColumnNameAttribute)] as ColumnNameAttribute; 
    if(att != null){ 
      e.Column.HeaderStyle = new Style(typeof(DataGridColumnHeader)); 
      e.Column.HeaderStyle.Setters.Add(new Setter(HorizontalContentAlignmentProperty, att.Alignment)); 
    } 

} 
0

我在搜索同一問題時登陸了此處行標題對齊。如果任何人在尋找,解決方案很簡單,只要:

<DataGrid.RowHeaderStyle> 
    <Style TargetType="DataGridRowHeader"> 
    <Style.Resources> 
     <Style TargetType="StackPanel"> 
     <Setter Property="HorizontalAlignment" Value="Center" /> 
     </Style> 
    </Style.Resources> 
    </Style> 
</DataGrid.RowHeaderStyle> 
0

應該是靜態資源,而不是DynamicResource在列:

風格

<Window.Resources> 
    <Style x:Key="CenterGridHeaderStyle" TargetType="DataGridColumnHeader"> 
     <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    </Style> 
</Window.Resources> 

<DataGridTextColumn 
    Binding="{Binding Path=Name}" Header="Name" IsReadOnly="True" 
    HeaderStyle="{StaticResource CenterGridHeaderStyle}"/>