在 WPF 中的 dataGridCells 上设置填充 [英] Set a padding on dataGridCells in WPF

查看:15
本文介绍了在 WPF 中的 dataGridCells 上设置填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题:如何在 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没有转移到BorderBorderBorder代码>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 上设置填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆