开发者

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>
&开发者_StackOverflow中文版lt;DataTemplate>
    <Grid>
        <Button x:Name="c_expandCollapseBtn" Cursor="Hand" Click="OnExpandCollapseClick" Content="+"
                MinHeight="8" MinWidth="15" VerticalAlignment="Top" HorizontalAlignment="Left"/>
    </Grid>
</DataTemplate> 
</DataGrid.RowHeaderTemplate>


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>


You might use Snoop tool - this will help you to debug the layout of your visual element. It seems the Grid itself is aligned to the center. Place VerticalAlignment="Top" to the grid.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜