2014-03-12 76 views
0

我已经WPF的DataGrid,在我的数据网格的列..如何在datagrid WPF中创建中心列文本?

<DataGrid.Columns> 
      ... 

      <mui:DataGridTextColumn x:Name="Column27" Width="50" Header="Cabe" Binding="{Binding B4R27,UpdateSourceTrigger=PropertyChanged ,Converter={StaticResource CheckConverter}, Mode=TwoWay}" /> 
      <mui:DataGridTextColumn IsReadOnly="True" x:Name="Column28" Width="50" Header="Jumlah Bahan Pokok" Binding="{Binding B4RJ,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" > 
       <DataGridTextColumn.CellStyle> 
        <Style TargetType="DataGridCell"> 
         <Setter Property="Background" Value="#A2D1A2" /> 
         <Setter Property="HorizontalAlignment" Value="Stretch" /> 
        </Style> 
       </DataGridTextColumn.CellStyle> 

       <DataGridTextColumn.ElementStyle> 
        <Style TargetType="TextBlock"> 
         <Setter Property="TextAlignment" Value="Center" /> 
        </Style> 
       </DataGridTextColumn.ElementStyle> 

      </mui:DataGridTextColumn> 
      <mui:DataGridTextColumn x:Name="Column29" Width="150" Header="Tulis Nama Pengusaha" Binding="{Binding B4R28,UpdateSourceTrigger=PropertyChanged , Mode=TwoWay}" /> 
      <mui:DataGridTextColumn x:Name="Column30" Width="130" Header="Tulis Alamat Lengkap" Binding="{Binding B4R29,UpdateSourceTrigger=PropertyChanged , Mode=TwoWay}" /> 
     </DataGrid.Columns> 

我可以让中心上方

 <DataGridTextColumn.ElementStyle> 
        <Style TargetType="TextBlock"> 
         <Setter Property="TextAlignment" Value="Center" /> 
        </Style> 
      </DataGridTextColumn.ElementStyle> 

使用这种风格与此代码我的DataGrid中文字,但我想在我所有的在我的datagrid列datagridtextcolumn。 我如何使它像风格,所以我所有的datagridtextcolumn都有相同的居中对齐文本?

回答

7

试试这个StyleDataGridColumnHeader

<DataGrid.Resources> 
    <Style TargetType="{x:Type DataGridColumnHeader}"> 
     <Setter Property="HorizontalContentAlignment" Value="Center" /> 
    </Style> 
</DataGrid.Resources> 

你也可以把他ColumnHeaderStyle

<DataGrid.ColumnHeaderStyle> 
    <Style TargetType="{x:Type DataGridColumnHeader}"> 
     <Setter Property="HorizontalContentAlignment" Value="Center" /> 
    </Style> 
</DataGrid.ColumnHeaderStyle> 

如果您有DataGridColumnHeader目前的风格,那么你需要使用的样式继承使用BasedOn这样的:

<Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" <--- Here may also be the key of your Style 
     TargetType="{x:Type DataGridColumnHeader}"> 

    <Setter Property="HorizontalContentAlignment" Value="Center" /> 
</Style> 

如果你想的到DataGridCell中心设置内容,然后用这个Style

<Style TargetType="{x:Type DataGridCell}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type DataGridCell}"> 
       <Grid Background="{TemplateBinding Background}"> 
        <ContentPresenter VerticalAlignment="Center" 
             HorizontalAlignment="Center" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

仍然在我的代码不会工作。 – mrhands

+0

我用https://mui.codeplex.com控制数据网格的..只有当我使用上面的代码时,我才能居中。如果我想以编程方式使用它,你能给我一个线索吗? – mrhands

+0

Sory我的意思是datagrid的内容而不是列标题。它是否也适用于'DataGridCell'? – mrhands