C# WPF datagrid - 自动列间距

标签 c# wpf datagrid spacing

我有一个包含列的数据网格。每列都有自动宽度(取决于内容)。我的问题是我想要每列之间有一个间距(填充?)。

我尝试使用以下代码来实现这一点:

 <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/

相关文章:

c# - TextBox RaiseEvent KeyDownEvent 不起作用...(附代码)

c# - 在 ControlTemplate 中设置按钮图像

c# - 为什么在 .NET 4.6 中更改 WPF DataGrid ItemsSource 如此缓慢?

c# - Visual Studio 2013.3 中新 MVC5 项目的错误

c# - 在 Entity Framework 中附加集合

c# 优先考虑扩展方法(仅限)

c# - 我如何 mock UserPrincipal?

wpf - ScrollViewer 上的动画(平滑)滚动

c# - DataGrid.CellStyle 适用于 C# WPF 应用程序中的整行

c# - 无法在新任务中填充数据网格