MahApps DataGridCell 样式

MahApps DataGridCell style

我在我的应用程序中使用 MahApps 库,datagrid 单元格的默认样式没有任何边框:

但我希望 DataGrid 看起来像这样:

所以我开始了:

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="Height"
            Value="33" />
    <Setter Property="Background"
            Value="Transparent" />
    <Setter Property="Padding"
            Value="5,0" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border x:Name="border"
                        Background="Transparent"
                        BorderBrush="Black"
                        BorderThickness="1"
                        Padding="0,10,0,10"
                        SnapsToDevicePixels="True">
                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Grid Background="{TemplateBinding Background}">
                    <ContentPresenter VerticalAlignment="Center" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

但没有任何变化,所以我假设我缺少 Based On={} 属性,但我不知道,我已经向 MathApps 团队寻求帮助,但没有帮助...

DataGrid 控件本身上设置 GridLinesVisibility="All" 将为您提供内部边框。

请注意,它仍然不会为您提供外部边框,但它比编辑默认模板容易得多。

如果你真的想获得外边框,那么你需要覆盖默认模板。

MahApps 默认的 DataGridCell 模板键是 MetroDataGridCell,因此您需要使用 <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource MetroDataGridCell}">