如何仅在 WPF 的 DataGrid 中的行下创建带有圆角的纯色背景

2023-12-29

我希望制作具有纯色背景且仅在行下带有圆角的数据网格。标题行必须没有背景。

我尝试了多种选项来通过(行/标题/数据网格)样式来达到此目的,但没有成功。

我想最好的方法是使用 DataTrigger 绑定到 DataGrid 行顺序(第一行和最后一行的一个模板,中间行的第二个模板),但我找不到行顺序属性。我将不胜感激所有的帮助。

它一定是这样的。


您必须覆盖数据网格的模板(听起来比实际复杂得多):

<Style TargetType="{x:Type DataGrid}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGrid}">
                <!-- extra attributes as example, but you can tweak whatever you want in the template, what is important is the "CornerRadius" here -->
                <Border Name="Border"
                        Padding="{TemplateBinding Padding}"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="1"
                        CornerRadius="3">
                    <ScrollViewer Name="DG_ScrollViewer" Focusable="false" CanContentScroll="True" IsDeferredScrollingEnabled="true">
                        <!-- here you can also overwrite the Scrollviewer's Template if you wish -->
                        <ItemsPresenter SnapsToDevicePixels="True" />
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

此样式应位于 /theme/Generic.xaml 或您的 app.xaml 中(或您想要的任何 xaml 字典中,只要正确引用即可)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何仅在 WPF 的 DataGrid 中的行下创建带有圆角的纯色背景 的相关文章

随机推荐