DataGrid RowHeaderTemplate中的垂直对齐方式

时间:2011-06-14 06:22:00

标签: .net wpf datatemplate wpfdatagrid

这是我使用的代码。我尝试将VerticalAlignment设置为Top,但是当显示行详细信息(展开以显示)时,按钮移动到行的中心。

<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>

2 个答案:

答案 0 :(得分:2)

您正在设置按钮内容的对齐方式。 下面是一种将行标题内容呈现器置于顶部的样式。

修改

    <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>

答案 1 :(得分:0)

您可以使用Snoop工具 - 这将帮助您调试视觉元素的布局。似乎网格本身与中心对齐。将VerticalAlignment =“Top”放置到网格中。