【问题标题】:WPF datagrid header/data align issuesWPF 数据网格标题/数据对齐问题
【发布时间】:2015-09-20 05:23:40
【问题描述】:

有一个 WPF 数据网格,没有什么特别之处。它有时有 HeadersVisibility All/Row,取决于我们代码中的用法。 但无论标题样式如何,它都有对齐问题。 标题/数据文本都没有正确对齐,左上角的第一个标题也没有正确对齐,这里缺少 1 个像素。 我附上一张图片以便更明显。

这看起来真的很不专业和业余。 文本对齐比左上角差很多,所以如果我能修复它已经是一个很大的改进。

垂直:正如我所见,行标题文本与中心正确对齐,不像上面的数据。

Horizo​​ntal:这里哪个与哪个(标题数据)对齐并不重要,但至少它们应该是内联的。

谢谢

编辑 XAML 部分:

<dg2d:DataGrid2DT.CellStyle>
    <Style TargetType="wpftoolkit:DataGridCell">
            <Setter Property="TextBlock.VerticalAlignment" Value="Stretch"/>
            <Setter Property="TextBlock.HorizontalAlignment" Value="Stretch"/>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="LightBlue"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</dg2d:DataGrid2DT.CellStyle>

我尝试添加&lt;Setter Property="TextBlock.Padding" Value="5, 5, 0, 0" /&gt;,但这没有任何效果。只是为了尝试,我添加了&lt;Setter Property="TextBlock.TextAlignment" Value="Right" /&gt; - 这很好用。只有paddig什么都不做。也许单元格太小而无法填充?

EDIT2

如果我将 VerticalAlignment 设置为“Center”,则 Textblock 将放置在单元格的中心。有了这个,文本或多或少在中心,但不好,因为文本块不再填充单元格。我希望文本块填充单元格,并使文本垂直居中。 使用“填充”应该可以解决我的问题,但它没有效果。

【问题讨论】:

  • 您是否尝试设置行标题或列标题的内容控件的VerticalAlingment 和/或“Horizo​​ntalAlignment”。您也可以尝试在您的 Cell 上设置它们
  • 但对齐问题不在于行或列标题,而在于数据单元格。看截图,与数据单元格不同,标题文本对齐良好。
  • 如何定义单元格?作为DataGridTemplateColumn?

标签: wpf datagrid header alignment


【解决方案1】:

您可以将 DataGridColumn 中的文本居中,例如:

<DataGridTemplateColumn Header="1">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Item1Property}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

【讨论】:

  • XAML 中未定义单元格。我认为它们是在代码中动态生成的。 (对不起,这不是我的代码,我只是接手,对wpf不熟悉)
  • 我在包含 DataTemplate 的 XAML 中有这个:&lt;dg2d:DataGrid2DT.RowHeaderTemplate&gt;&lt;DataTemplate&gt;&lt;TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type wpftoolkit:DataGridRow}},Path=Header}"/&gt;&lt;/DataTemplate&gt;&lt;/dg2d:DataGrid2DT.RowHeaderTemplate&gt;
  • 在 xaml 中,我只看到 RowHeader 的 DataTemplate 而不是特定列。我以为列是你的问题???
  • 我的问题是单元格文本既不与行标题文本垂直对齐,也不与列标题文本水平对齐。看上图,看到红线不是连续的。
  • 还有AutoGeneratingColumn的事件处理器,这里设置了column.Binding.Path
【解决方案2】:

我找到了解决方案,通过使用Padding 我可以将单元格文本与标题文本对齐。 然而,正如我所提到的,使用简单的PaddingTextBlock.Padding 不起作用,你需要一个“技巧”,我在这里找到了它: Set a padding on dataGridCells in WPF

<dg2d:DataGrid2DT.CellStyle>
    <Style TargetType="wpftoolkit:DataGridCell">
        <Setter Property="TextBlock.VerticalAlignment" Value="Stretch"/>
        <Setter Property="TextBlock.HorizontalAlignment" Value="Stretch"/>

--> solution
        <Setter Property="Padding" Value="2,2,0,0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type wpftoolkit:DataGridCell}">
                    <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
<-- solution

        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="LightBlue"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</dg2d:DataGrid2DT.CellStyle>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多