我有一个包含列的数据网格。每列都有自动宽度(取决于内容)。我的问题是我想要每列之间有一个间距(填充?)。
我尝试使用以下代码来实现这一点:
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Padding" Value="0 0 20 0"/>
<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>
它基本上可以工作,但如果我选择一个单元格,则不会选择 20px(填充)(这只是一个设计问题)。
它看起来并不漂亮。有任何想法吗?
最佳答案
尝试将样式添加到每一列并在那里定义填充。类似于 Style="{StaticResource InfoNameTextBlock}"
在保持风格的类里面
<Style TargetType="yourType" x:Key="InfoNameTextBlock">
<Setter Property="Padding" Value="5,0"/>
</Style>
关于C# WPF datagrid - 自动列间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33473477/