2014-01-22 59 views
2

我有此XAML代碼:爲什麼單元格標題和單元格內容未在數據網格中對齊?

  <DataGridTextColumn Header="number" Binding="{Binding Path=Number, Mode=OneWay}" Width="70" CanUserResize="False" > 
       <DataGridTextColumn.CellStyle> 
        <Style> 
         <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center" /> 
        </Style> 
       </DataGridTextColumn.CellStyle> 
      </DataGridTextColumn> 

      <DataGridTextColumn Header="Status" Binding="{Binding Path=ProcessingState}" Width="100"> 
       <DataGridTextColumn.CellStyle> 
        <Style> 
         <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center" /> 
        </Style> 
       </DataGridTextColumn.CellStyle> 


      </DataGridTextColumn> 

但是當它顯示在屏幕上,它生成這樣的觀點:

enter image description here

如可以看到的,該細胞是不對齊與標題。我如何讓他們對齊?

回答

2

如果你只是想中心在單元格中的文本,那麼你可以使用一個DataGridTemplateColumn這樣的:

<DataGrid ItemsSource="{Binding SalesPeriods}" AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
     <DataGridTemplateColumn Header="number" Width="70" CanUserResize="False"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding Path=Number, Mode=OneWay}" 
         HorizontalAlignment="Center" /> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
     <DataGridTemplateColumn Header="Status" Width="100"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding Path=ProcessingState, Mode=OneWay}" 
         HorizontalAlignment="Center" /> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

如果你喜歡使用DataGridTextColumn,那麼你仍然可以實現使用ElementStyle這一目標物業編號:

<DataGridTextColumn Header="number" Binding="{Binding Path=Year, Mode=OneWay}" 
    Width="70" CanUserResize="False" > 
    <DataGridTextColumn.ElementStyle> 
     <Style TargetType="{x:Type TextBlock}"> 
      <Setter Property="HorizontalAlignment" Value="Center" /> 
     </Style> 
    </DataGridTextColumn.ElementStyle> 
</DataGridTextColumn> 
相關問題