删除wpf datagrid上的所有边框

时间:2017-07-10 17:15:09

标签: c# wpf datagrid

我对WPF有点新意,我无法找到彻底摆脱单元格中所有边界,填充和边距的方法。我想要的是桌子上没有白色,只有一条简单的水平线。

以下是我得到的截图:

enter image description here

以下是我想要的截图:

enter image description here

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

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

但我发现无法完全删除我不想要的所有内容。 任何线索?

1 个答案:

答案 0 :(得分:3)

您已经拥有GridLinesVisibility="Horizontal"

您还需要的属性是BorderThickness

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

制作默认的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>