在WPF中为DataGrid单元格设置填充。

75
简单问题:如何在WPF的DataGridCell中设置填充(padding)?(无论是一次性设置还是全部设置,我都无所谓)
我尝试使用DataGrid.CellStyle属性,通过添加DataGridCell.Padding属性的setter以及使用DataGridColumn.CellStyle属性来进行设置,但没有效果。
我还尝试使用DataGridColumn.ElementStyle属性,但仍然没有成功。
我有些困惑,请问是否有人成功地在DataGridCell上应用了padding?
注:我要补充说明,不能使用透明边框来实现这个效果,因为我已经将边框属性用于其他用途。我也不能使用margin属性(虽然出乎意料地可以工作),因为我使用了背景属性,而不想在我的单元格之间留下任何“空白”空间。
5个回答

146
问题在于Padding没有传递到DataGridCell模板中的Border。您可以编辑模板并添加PaddingTemplateBinding
<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>

4
好的,我接受了你的答案,尽管我找到了另一种方法(有点像黑客,但在我的情况下更容易):通过使用ElementStyle属性,在单元格内容上设置边距属性。不过你的解决方案更好。 - David
2
@David:是的,那将是另一种方法。无论什么方式能完成工作:) - Fredrik Hedblad
16
我会补充说,这让我觉得 WPF 还远没有完成,就我而言... - David
2
@FredrikHedblad,您是如何知道在Border上应用TemplateBinding的哪些属性的呢? - echo
3
我已经为WPF团队提交了一个问题,希望让这种情况更容易处理:https://github.com/dotnet/wpf/issues/2874 - Kirill Osenkov
显示剩余2条评论

33

这是一种更干净的方法(我的观点),结合了David的方法

<Resources>
    <Style x:Key="ColumnElementStyle" TargetType="TextBlock">
        <Setter Property="Margin" Value="5,0,10,0" />
    </Style>
</Resources>

那么...

<DataGridTextColumn ElementStyle="{StaticResource ColumnElementStyle}" />
<DataGridTextColumn ElementStyle="{StaticResource ColumnElementStyle}" />
在我的情况下,我的行是只读的,因此没有编辑样式。

14

近五年后,由于这个问题似乎仍然有用(它仍然得到赞同),并且因为被请求,这里是我在TextColumn上使用ElementStyle解决方案(但您可以为任何类型的DataGridColumn执行相同操作):

我全部在代码后台完成:

class MyTextColumn : DataGridTextColumn
{
    public MyTextColumn()
    {
        ElementStyle = new Style(typeof(TextBlock));
        EditingElementStyle = new Style(typeof(TextBox));

        ElementStyle.Setters.Add(new Setter(FrameworkElement.MarginProperty, new Thickness(3)));
        EditingElementStyle.Setters.Add(new Setter(Control.PaddingProperty, new Thickness(0, 1, 0, 1)));
    }
}

但如果您想直接在 XAML 中进行操作:

<DataGrid.Columns>
    <DataGridTextColumn>
        <DataGridTextColumn.ElementStyle>
            <Style TargetType="TextBlock">
                <Setter Property="Margin" Value="3"/>
            </Style>
        </DataGridTextColumn.ElementStyle>
        <DataGridTextColumn.EditingElementStyle>
            <Style TargetType="TextBox">
                <Setter Property="Padding" Value="0 1 0 1"/>
            </Style>
        </DataGridTextColumn.EditingElementStyle>
    </DataGridTextColumn>
</DataGrid.Columns>

3
你可以尝试更改

{Binding BindingValue, StringFormat={}{0:#0.0000}}

为了

{Binding BindingValue, StringFormat={}{0:#0.0000 }}

有趣的是,WPF的XAML {0:#0.0000}将使用这个额外的空格字符以所渲染控件的格式来对你的值进行移动,使其不会超出网格列的边缘。


3
<DataGrid.Columns>
      <DataGridTextColumn  MinWidth="100" Header="Changed by"  Width=""  Binding="{Binding Changedby}" IsReadOnly="True"  >
        <DataGridTextColumn.CellStyle>
          <Style TargetType="DataGridCell">
          <Setter Property="BorderThickness" Value="0"/>
          <Setter Property="Background" Value="Transparent" />
         <Setter Property="FrameworkElement.HorizontalAlignment"Value="Center"/>
          </Style>
      </DataGridTextColumn.CellStyle>
    </DataGridTextColumn>


请解释。 - Scover

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接