【问题标题】:How to adjust DataGrid column with order numbers WPF?如何使用订单号 WPF 调整 DataGrid 列?
【发布时间】:2020-01-27 12:28:00
【问题描述】:

我有这样的DataGrid

为了自动获取订单号,我在DataGrid方法中添加了LoadingRow这样的方式

.xalm

...
<DataGrid ItemsSource="{Binding Path=GridItems}"
            HorizontalGridLinesBrush="Gray"
            RowHeaderWidth="20"
            LoadingRow="Dg_main_configuration_LoadingRow"
            CanUserReorderColumns="False"
            CanUserDeleteRows="False"
            CanUserResizeRows="False"
            CanUserSortColumns="False"
            VerticalGridLinesBrush="LightGray"
            x:Name="Dg_main_configuration"
            CanUserResizeColumns="False"
            PreviewMouseLeftButtonUp="Dg_main_configuration_PreviewMouseLeftButtonUp"
            AlternatingRowBackground="LightYellow"
            CanUserAddRows="False"
            MinHeight="350"
            MaxHeight="350"
            Grid.Column="0"
            AutoGenerateColumns="False">
...

在代码中

private void Dg_main_configuration_LoadingRow(object sender, DataGridRowEventArgs e)
{
    e.Row.Header = (e.Row.GetIndex() + 1).ToString();
}

一切都很好,但无论如何我想调整这一行列

问题是:

  1. 您可以在屏幕截图中看到Path to clip folder(左侧)右下角有一个带有矩形的单元格,实际上它是一种按钮(因为我可以点击它,但没有任何改变),如何禁用它,或设置符号如#?
  2. 数字1, 2, 3, 4水平不在中心,如何解决?
  3. 最后是 - 你可以看到有一个水平线DarkGray 分隔行,但我不知道为什么数字不包括这个单独的行?我的意思是行在数字列之后开始。如何在订单号中也包含单独的行?

【问题讨论】:

  • 额外的列而不是行标题怎么样?像这样:stackoverflow.com/a/15061668/1136211
  • 也许只是 &lt;DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType=DataGridRow}}"/&gt; 在 DataGrid 中设置了较大的 AlternationCount 值。
  • @Clemens 看起来可行。但是如何设置对齐方式(垂直和水平)?
  • 通过 DataGridTemplateColumn 中的适当 CellTemplate
  • @Clemens 可以看看这个问题stackoverflow.com/q/59933150/5709159

标签: c# wpf


【解决方案1】:

您可以在一列中显示行号,例如使用提到的方法here

然后您可以使用ElementStyleTextBlock 居中:

<DataGridTextColumn Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, 
                        Converter={local:RowToIndexConverter}}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="TextBlock">
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

【讨论】:

    【解决方案2】:

    对于第 2 点(也可能让您更了解第 3 点)。 DataGrid 可以有一个RowHeaderTemplate(或更通用的RowHeaderStyle),它将应用于每个行标题值(您在代码中设置)。这是related question.

    对于第 1 点(以及对于默认行标题模板的第 2 点和第 3 点)我建议您查看 Microsoft 的默认 DataGrid styles and templates。您可能需要设置整个 DataGrid 模板以更改左上角部分(或尝试使用键为 {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}} 的资源) - 这就是 DataGridSelectAllButtonStyle,它应该是如果可以在数据网格中选择行,则该按钮将选择所有行。

    对于第 3 点,要将水平网格线设置为不同的颜色(例如匹配行标题的分隔符),请使用 HorizontalGridLinesBrush 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-20
      • 2016-01-22
      • 2011-08-20
      • 2011-04-30
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多