简单的问题:如何在 WPF 中的 dataGridCell 上设置填充?(一次一个或在所有单元格上,我不在乎)
我尝试通过在DataGrid.CellStyle
属性上添加一个设置器来DataGridCell.Padding
使用该属性,并以相同的方式使用该DataGridColumn.CellStyle
属性但没有任何效果。
我也尝试使用该DataGridColumn.ElementStyle
属性,但没有更多运气。
我有点卡在那里,有没有人设法在dataGridCell上应用填充?
注意:我要补充一点,不,我不能使用透明边框来执行此操作,因为我已经将边框属性用于其他用途。我也不能使用 margin 属性(这似乎可以工作,令人惊讶的是),因为我使用了 background 属性并且我不希望我的单元格之间有任何“空白”空间。
问题是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>
好的,我接受了你的回答,尽管我找到了另一种方法(有点像黑客,但在我的情况下更容易):使用 ElementStyle 属性设置单元格内容的边距属性。不过,您的解决方案更好。
@David:是的,这将是另一种方式。什么都能完成工作:)
我要补充一点,就我而言,这是另一件让我认为 WPF 还远未完成的事情......
@FredrikHedblad 你怎么知道要在边框上应用 TemplateBinding 的哪些属性?
我已经为 WPF 团队提交了一个问题,以使这种情况更容易:github.com/dotnet/wpf/issues/2874