【问题标题】:Vertical alignment in DataGrid RowHeaderTemplateDataGrid RowHeaderTemplate 中的垂直对齐
【发布时间】:2011-06-14 06:21:59
【问题描述】:

这是我使用的代码。我尝试将 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>

【问题讨论】:

    标签: .net wpf datatemplate wpfdatagrid


    【解决方案1】:

    您正在设置按钮内容的对齐方式。 这是一种将行标题内容展示器置于顶部的样式。

    编辑

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

    【讨论】:

    • 谢谢,但在将 VerticalAlignment 设置为网格后,它仍然显示在中心
    • 我看到当我在 vs 中检查它时,我编辑了我的答案以包含样式......
    • 好的。在发表我的评论之前,我没有注意到代码。我尝试了你提供的代码,它对我有用。谢谢!!!
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2011-04-28
      • 2016-08-10
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 2015-12-29
      • 2013-02-13
      • 1970-01-01
      相关资源
      最近更新 更多