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

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

问题描述

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



我尝试使用 DataGrid.CellStyle通过在 DataGridCell.Padding 属性上添加一个setter,以及使用 DataGridColumn.CellStyle 属性以相同的方式没有任何效果。



我还尝试使用 DataGridColumn.ElementStyle 属性,没有更好的运气。



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



注意:我会补充说,不,我不能使用透明边框来做到这一点,因为我已经使用边框属性的其他东西。我也不能使用margin属性(这似乎非常有用),因为我使用background属性,我不希望我的单元格之间有任何空白空格。

解决方案

问题是, Padding 不会转移到 Border DataGridCell 的模板中。您可以编辑模板并添加TemplateBinding for Padding

  DataGrid ...> 
< DataGrid.CellStyle>
< Style TargetType =DataGridCell>
< Setter Property =PaddingValue =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>


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)

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.

I also tried using the DataGridColumn.ElementStyle property with no more luck.

I'm kind of stuck there, has anyone managed to get a padding applied on a dataGridCell?

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.

解决方案

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天全站免登陆