2011-10-30 43 views
11

Tôi có một DataGrid như thế này:Làm thế nào để Thiết lập phong cách cho tiêu đề DataGrid trong WPF

<DataGrid AutoGenerateColumns="False" Height="221" HorizontalAlignment="Center" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" 
       Margin="6,269,0,0" Name="dataGrid1" VerticalAlignment="Center" Width="875" SelectionChanged="dataGrid1_SelectionChanged"> 
     <DataGrid.Columns > 
      <DataGridTextColumn x:Name="Id" Header="ID" Binding="{Binding Path=Key}" HeaderStyle=""> 
      </DataGridTextColumn> 
      <DataGridTemplateColumn Header="Image" Width="SizeToCells" IsReadOnly="True" MinWidth="80"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <Image Width="16" Height="16" Source="{StaticResource MyImageSource}" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
    </DataGrid> 

Làm thế nào tôi có thể tập trung DataGrid Header? và áp dụng phong cách cho nó?

nhờ


Sửa 1):

sau khi ghi mã này:

cột lưới
<DataGrid.ColumnHeaderStyle> 
      <Style TargetType="{x:Type DataGridColumnHeader}"> 
       <Setter Property="FontWeight" Value="Bold" /> 
       <Setter Property="HorizontalAlignment" Value="Center" /> 
       <Setter Property="HorizontalContentAlignment" Value="Center" /> 
      </Style> 
     </DataGrid.ColumnHeaderStyle> 

trở thành:

enter image description here

Tại sao?

Trả lời

14

Để tránh ảnh hưởng của tiêu đề sụp đổ chỉ đơn giản là sửa phong cách của bạn theo cách này:

<Setter Property="HorizontalAlignment" Value="Stretch" /> 
    <Setter Property="HorizontalContentAlignment" Value="Center" /> 
+4

Không cần cho là "Stretch" là mặc định. –

Các vấn đề liên quan