如何垂直居中行? WPF DataGrid 中的内容?

发布于 2024-12-18 19:58:04 字数 870 浏览 2 评论 0原文

我想在DataGrid中使数据记录的VerticalAlignment=Center。默认情况下,数据记录的VerticalAlignment=Top,看起来很难看。可以给我这个款式吗?

我在 App.xaml 文件中定义我的样式。以下是我当前的DataGrid样式:

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぇ气 2024-12-25 19:58:04

尝试在 DataGrid 上设置 VerticalContentAlignment="Center"

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

或者您可以向样式添加 setter:

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

当应用于 ItemsControl 时,此属性通常会修改每个单独项目容器的对齐方式。在您的情况下,这应该使所有行将其内容对齐到中心。

更新

似乎 WPF 内置 DataGrid 不遵循规则。

该解决方案取决于您使用的色谱柱的类型。

对于 DataGridTemplateColumn 使用 CellTemplate,如下所示:

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

对于 DataGridTextColumn,设置 ElementStyle

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

我已在 上尝试过此操作>DataGridTextColumn,但该属性来自其父级 DataGridBoundColumn,因此也适用于 DataGridCheckBoxColumnDataGridHyperlinkColumn

更新 2

顺便说一句,还有另一种解决方案

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文