问题描述
简单的问题:如何在 WPF 中的 dataGridCell 上设置填充?(一次一个或所有单元格,我不在乎)
simple question: How can I set a padding on a dataGridCell in WPF?(either one at a time or on all cells, I don't care)
我已经尝试通过在 DataGridCell.Padding
属性上添加一个 setter 以及使用 DataGridColumn.CellStyle
来使用 DataGrid.CellStyle
属性> 属性相同,没有效果.
I have tried using the DataGrid.CellStyle
property by adding a setter on the DataGridCell.Padding
property as well as using the DataGridColumn.CellStyle
property in the same way with no effect.
我也尝试过使用 DataGridColumn.ElementStyle
属性,但没有成功.
I also tried using the DataGridColumn.ElementStyle
property with no more luck.
我有点卡在那里,有没有人设法在 dataGridCell 上应用填充?
I'm kind of stuck there, has anyone managed to get a padding applied on a dataGridCell?
注意:我要补充一点,不,我不能使用透明边框来执行此操作,因为我已经将边框属性用于其他用途.我也不能使用 margin 属性(这似乎有效,令人惊讶的是),因为我使用了 background 属性,我不希望我的单元格之间有任何空白"空间.
NB: I'll add that no, I cannot use transparent borders to do this, since I already use the border properties for something else. I also cannot use the margin property (which seems to work, surprisingly enough) as I use the background property and I don't want any "blank" space between my cells.
推荐答案
问题是Padding
没有转移到Border
的Border
的Border
代码>DataGridCell.您可以编辑模板并为 Padding
The problem is that the Padding
isn't transfered to the Border
that's in the Template for DataGridCell
. You can edit the Template and add the TemplateBinding for Padding
<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>
这篇关于在 WPF 中的 dataGridCells 上设置填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!