Search code examples
.netwpfdatatemplatewpfdatagrid

Vertical alignment in DataGrid RowHeaderTemplate


Here is the code I used. I tried setting VerticalAlignment to Top, but when the row details are shown (expand to show), the button moves to the center of the row.

<DataGrid.RowHeaderTemplate>
<DataTemplate>
    <Grid>
        <Button x:Name="c_expandCollapseBtn" Cursor="Hand" Click="OnExpandCollapseClick" Content="+"
                MinHeight="8" MinWidth="15" VerticalAlignment="Top" HorizontalAlignment="Left"/>
    </Grid>
</DataTemplate> 
</DataGrid.RowHeaderTemplate>

Solution

  • you are setting the alignment of the buttons content. heres a style that places the row header content presenter at the Top.

    EDIT

        <Style x:Key="DataGridRowHeaderStyle" TargetType="{x:Type DataGridRowHeader}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridRowHeader}">
                        <Grid>
                            <Microsoft_Windows_Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" IsSelected="{TemplateBinding IsRowSelected}" Orientation="Horizontal" Padding="{TemplateBinding Padding}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}">
                                <StackPanel Orientation="Horizontal">
                                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Top"/>
                                    <Control SnapsToDevicePixels="false" Template="{Binding ValidationErrorTemplate, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Visibility="{Binding (Validation.HasError), Converter={StaticResource bool2VisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}"/>
                                </StackPanel>
                            </Microsoft_Windows_Themes:DataGridHeaderBorder>
                            <Thumb x:Name="PART_TopHeaderGripper" Style="{StaticResource RowHeaderGripperStyle}" VerticalAlignment="Top"/>
                            <Thumb x:Name="PART_BottomHeaderGripper" Style="{StaticResource RowHeaderGripperStyle}" VerticalAlignment="Bottom"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>