简体   繁体   中英

MahApps DataGridCell style

I'm using MahApps library in my app, the default style of datagrid cell doesn't have any borders:

无边界的数据网格单元

but I want the DataGrid to look like this:

带有边界的数据网格单元

So I've started with:

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

but nothing changes, so I assume that I'm missing Based On={} attribute but I have no clue, I've asked for help MathApps team but no help...

Setting GridLinesVisibility="All" on the DataGrid control itself will give you the inner borders.

在此处输入图片说明

Note it doesn't give you the outside borders still, but it's a lot easier than editing the default template.

If you really want to get the outside borders, then you'll need to override the default template.

The MahApps default DataGridCell template key is MetroDataGridCell , so you will need to use <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource MetroDataGridCell}">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM