3

I want to make the data record's VerticalAlignment=Center in DataGrid. By default,the data record's VerticalAlignment=Top, It looks uglily. Can you provide me this style?

I define my style in App.xaml file. The following is my current DataGrid style:

    <Style x:Key="DataGridView" TargetType="DataGrid">
        <Setter Property="AlternatingRowBackground" Value="AliceBlue"></Setter>
        <Setter Property="AlternationCount" Value="1"></Setter>
        <Setter Property="AutoGenerateColumns" Value="False"></Setter>
        <Setter Property="GridLinesVisibility" Value="Horizontal"></Setter>
        <Setter Property="VerticalGridLinesBrush" Value="DarkGray"></Setter>
        <Setter Property="HorizontalGridLinesBrush" Value="DarkGray"></Setter>
        <Setter Property="RowHeight" Value="32"></Setter>
    </Style>
ajreal
  • 46,720
  • 11
  • 89
  • 119
qing xu
  • 55
  • 1
  • 1
  • 3

1 Answers1

8

Try setting VerticalContentAlignment="Center" on the DataGrid:

<DataGrid VerticalContentAlignment="Center">
  ...
</DataGrid>

or you can add a setter to your style:

<Setter Property="VerticalContentAlignment" Value="Center"/>

When applied to ItemsControls, this property usually modifies alignment of each individual item container. In your case, this should make all rows align their contents to center.

UPDATE

Seems like the WPF built-in DataGrid doesn't follow the rule.

The solution depends on the type of the columns you use.

For DataGridTemplateColumn use a CellTemplate like this:

<DataTemplate>
    <!--Substitute the TextBlock by the actual cell content, but don't drop VerticalAlignment-->
  <TextBlock VerticalAlignment="Center" Text="{Binding Text}"/>
</DataTemplate>

For DataGridTextColumn, set ElementStyle:

<Style>
  <Setter Property="FrameworkElement.VerticalAlignment" Value="Center"/>
</Style>

I've tried this on DataGridTextColumn, but the property is from its parent DataGridBoundColumn, so should work for DataGridCheckBoxColumn and DataGridHyperlinkColumn also.

Update 2

BTW, there's another solution.

Community
  • 1
  • 1
Pavel Gatilov
  • 7,579
  • 1
  • 27
  • 42
  • Hi Pavel Gatilov,I try to add the VerticalContentAlignment="Center" in my style setters list, but it seems no effect. – qing xu Nov 30 '11 at 03:31
  • By the way,the columns in DataGrid were created by my csharp code,not autogenerated by control itself. Because my ItemsSource is DataView and I can't know the columns which I want to display(They're come from database and the columns are dynamic). – qing xu Nov 30 '11 at 03:33
  • @qingxu Please, see my updated post. I've tried those two options myself and they do work. The only thing is you probably won't be able to include this into the `DataGrid`'s `Style`, but you should be able to assign the properties when you create your columns. – Pavel Gatilov Nov 30 '11 at 05:25
  • thanks for your solution,I follow your suggestion and create the DataGridTemplateColumn instead of DataGridTextColumn,it works.As you said,the VerticalContentAlignment can work on DataGridTemplateColumn,DataGridCheckboxColumn and others except DataGridTextColumn. – qing xu Nov 30 '11 at 05:42