Warm tip: This article is reproduced from serverfault.com, please click

datagrid-在 WPF 中的 dataGridCells 上设置填充

(datagrid - Set a padding on dataGridCells in WPF)

发布于 2011-03-09 13:52:39

简单的问题:如何在 WPF 中的 dataGridCell 上设置填充?(一次一个或在所有单元格上,我不在乎)

我尝试通过在DataGrid.CellStyle属性上添加一个设置器来DataGridCell.Padding使用该属性,并以相同的方式使用该DataGridColumn.CellStyle属性但没有任何效果。

我也尝试使用该DataGridColumn.ElementStyle属性,但没有更多运气。

我有点卡在那里,有没有人设法在dataGridCell上应用填充?

注意:我要补充一点,不,我不能使用透明边框来执行此操作,因为我已经将边框属性用于其他用途。我也不能使用 margin 属性(这似乎可以工作,令人惊讶的是),因为我使用了 background 属性并且我不希望我的单元格之间有任何“空白”空间。

Questioner
David
Viewed
0
Fredrik Hedblad 2011-03-09 22:18:59

问题是Padding没有转移到Border模板中的DataGridCell. 你可以编辑模板并添加 TemplateBindingPadding

<DataGrid ...>
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Padding" Value="20"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type 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>
        </Style>
    </DataGrid.CellStyle>
    <!--...-->
</DataGrid>