我对WPF有点陌生,我找不到完全摆脱单元格中所有边界,填充和边距的方法。我想要的是桌子上完全没有白色,鱼子之间只有一条简单的水平线。

这是我得到的屏幕截图:

c# - 删除WPF DataGrid上的所有边框-LMLPHP

这是我想要得到的屏幕截图:

c# - 删除WPF DataGrid上的所有边框-LMLPHP

到目前为止,我有这个简化的代码:

<DataGrid IsReadOnly="True" AllowDrop="True" ItemsSource="{Binding Mode=OneWay, Source={StaticResource imageInfoListView}}"
              AutoGenerateColumns="False" HorizontalScrollBarVisibility="Auto" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" ColumnWidth="*"
              FontSize="10" GridLinesVisibility="Horizontal" CanUserResizeColumns="False"
              CanUserResizeRows="False" BorderThickness="0">
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="Margin" Value="0"/>
                <Setter Property="Padding" Value="0"/>
            </Style>
        </DataGrid.CellStyle>
        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <TextBlock TextWrapping="WrapWithOverflow" Text="{Binding}" TextAlignment="Center" FontSize="9"/>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.ColumnHeaderStyle>
            <DataGridCheckBoxColumn Header="Guides" Binding="{Binding Guides}">
                <DataGridCheckBoxColumn.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="Background" Value="{Binding GuidesBrush}"/>
                    </Style>
                </DataGridCheckBoxColumn.CellStyle>
            </DataGridCheckBoxColumn>
            <DataGridTextColumn Header="Width" Binding="{Binding Width}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Background" Value="{Binding WidthBrush}"/>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Height" Binding="{Binding Height}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Background" Value="{Binding HeightBrush}"/>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Resolution" Binding="{Binding Resolution}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Background" Value="{Binding ResolutionBrush}"/>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Icc Model" Binding="{Binding IccModel}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Background" Value="{Binding IccModelBrush}"/>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Compression" Binding="{Binding Compression}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Background" Value="{Binding CompressionBrush}"/>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Errors" Binding="{Binding Errors}" Visibility="Hidden" />
        </DataGrid>


但是我找不到完全删除不需要的内容的方法。
有什么线索吗?

最佳答案

您已经有GridLinesVisibility="Horizontal"

您还需要的属性是BorderThickness

但是另一个问题是您定义了DataGrid.CellStyle,然后定义了不相关的DataGridTextColumn.CellStyle

设置默认的DataGridCell样式并在CellStyles列中使用BasedOn属性:

<DataGrid.Resources>
    <Style TargetType="DataGridCell">
        <Setter Property="Margin" Value="0"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="BorderThickness" Value="0"/>
    </Style>
</DataGrid.Resources>
<!--...-->
<DataGridTextColumn.CellStyle>
    <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
        <Setter Property="Background" Value="{Binding WidthBrush}"/>
    </Style>
</DataGridTextColumn.CellStyle>

关于c# - 删除WPF DataGrid上的所有边框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45018037/

10-10 14:52