简体   繁体   English

删除wpf datagrid上的所有边框

[英]Removing all borders on wpf datagrid

I am a bit new to WPF and I can't find a way to get totally rid of all borders, padding and margin in the cells. 我对WPF有点新,我无法找到完全摆脱单元格中所有边界,填充和边距的方法。 What I would like is to have no white at all in the table and only a simple horizontal line between the roes. 我想要的是桌子上没有白色,只有一条简单的水平线。

Here is a screenshot of what I get: 这是我得到的截图:

在此输入图像描述

Here is a screenshot of what I would like to get: 这是我想要的截图:

在此输入图像描述

So far I have this simplified code: 到目前为止,我有这个简化的代码:

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

But I find no way to completely remove everything I don't want. 但我发现没办法完全删除我不想要的东西。 Any clue? 任何线索?

you already have GridLinesVisibility="Horizontal" 你已经有GridLinesVisibility="Horizontal"

the property which you also need is BorderThickness 您还需要的属性是BorderThickness

but another issue is that you defined DataGrid.CellStyle and then unrelated DataGridTextColumn.CellStyle s 但另一个问题是你定义了DataGrid.CellStyle然后不相关的DataGridTextColumn.CellStyle s

make a default DataGridCell style and use BasedOn property in column CellStyles: 创建默认的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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