-1

I use WPF (C #). I use DataGrid. I want the first column is aligned with the center, the other columns are right-aligned.

I general have style:

<Style x:Key="TextInCellCenter" TargetType="{x:Type TextBlock}" >
     <Setter Property="HorizontalAlignment" Value="Center"/>
</Style>

<Style TargetType="{x:Type DataGridCell}">       
     <Setter Property="HorizontalAlignment" Value="Right"/>
</Style>

DataGrids:

<DataGrid Name="DG1">
  <DataGrid.Columns>
    <DataGridTextColumn ElementStyle="{StaticResource TextInCellCenter}" Binding="{Binding Path=Name}" />
    <DataGridTextColumn Binding="{Binding Path=Number}" />
    <DataGridTextColumn Binding="{Binding Path=Number}" />
    ....
    <DataGridTextColumn Binding="{Binding Path=Number}" />
    <DataGridTextColumn Binding="{Binding Path=Number}" />
  </DataGrid.Columns>
</DataGrid>

<DataGrid Name="DG2">
  <DataGrid.Columns>
    <DataGridTextColumn ElementStyle="{StaticResource TextInCellCenter}" Binding="{Binding Path=Name}" />
    <DataGridTextColumn Binding="{Binding Path=Number}" />
    <DataGridTextColumn Binding="{Binding Path=Number}" />
    ...
    <DataGridTextColumn Binding="{Binding Path=Number}" />
    <DataGridTextColumn Binding="{Binding Path=Number}" />
  </DataGrid.Columns>
</DataGrid>
....

I have all the columns are right-aligned.

Please tell me, how do I change the first column had a center text-alignment?

p.s. How to do it in a rational way? I've got a lot of similar tables.

Olga
  • 1,395
  • 2
  • 25
  • 34

1 Answers1

2

Instead of changing style of DataGridCell you can change ElementStyle for TextBlock and change TextAlignment to be Right or Center and apply it on per column basis

<DataGridTextColumn Binding="{Binding Path=Number}" >
   <DataGridTextColumn.ElementStyle>
      <Style TargetType="{x:Type TextBlock}">
         <Setter Property="TextAlignment" Value="Right"/>
      </Style>
   </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

or create this Style in DataGrid.Resources so you can apply to more then one column

<DataGrid>
    <DataGrid.Resources>
        <Style x:Key="TextInCellCenter" TargetType="{x:Type TextBlock}" >
            <Setter Property="TextAlignment" Value="Center"/>
        </Style>
        <Style TargetType="{x:Type TextBlock}" x:Key="RightAligElementStyle">
            <Setter Property="TextAlignment" Value="Right"/>
        </Style>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn ElementStyle="{StaticResource TextInCellCenter}" Binding="{Binding Path=Name}" />
        <DataGridTextColumn Binding="{Binding Path=Number}" ElementStyle="{StaticResource RightAligElementStyle}" />
        <DataGridTextColumn Binding="{Binding Path=Number}" ElementStyle="{StaticResource RightAligElementStyle}"/>
        <DataGridTextColumn Binding="{Binding Path=Number}" ElementStyle="{StaticResource RightAligElementStyle}"/>
    </DataGrid.Columns>        
</DataGrid>
dkozl
  • 32,814
  • 8
  • 87
  • 89
  • thanks, but the first method does not work, and second method is inconvenient, because I have more than 10 tables in which a lot of adding identical columns (like column "Number"). Define a style for each same column takes a lot of time. – Olga Jun 06 '14 at 10:30