WPF DataGrid日志展示场景下如何彻底去除单元格边角边框及网格线
要完全移除DataGrid内部所有边框和网格线,需要同时修改单元格、行、列头三个层级的样式,你现有代码的核心问题是CellStyle里误将BorderThickness设为了5,且缺少行样式、列头分隔线的隐藏配置,按以下修改即可:
具体修改点
- 将
DataGrid.CellStyle中的BorderThickness值从5改为0,避免单元格自带边框占位 - 新增
DataGrid.RowStyle配置,隐藏行层级的边框和选中状态的默认边框 - 列头样式中追加
SeparatorBrush透明配置,去掉列头之间的默认分隔线 - 列头样式统一设置
BorderThickness为0,消除列头自身边框
修改后完整XAML代码
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="216" HorizontalAlignment="Left" ItemsSource="{Binding FilteredEventLogList}" Background="{StaticResource PageBackgroundColor}" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="614" RowHeaderWidth="0" GridLinesVisibility="None" BorderThickness="1.5,1.5,1.5,1.5" BorderBrush="Gray" > <!-- 新增行样式,隐藏行边框和选中边框 --> <DataGrid.RowStyle> <Style TargetType="{x:Type DataGridRow}"> <Setter Property="BorderThickness" Value="0"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="Background" Value="{StaticResource PageBackgroundColor}"/> <!-- 覆盖选中状态的默认边框 --> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="Background" Value="{StaticResource PageBackgroundColor}"/> </Trigger> </Style.Triggers> </Style> </DataGrid.RowStyle> <DataGrid.CellStyle> <Style TargetType="{x:Type DataGridCell}"> <Setter Property="Background" Value="{StaticResource PageBackgroundColor}"/> <Setter Property="FontSize" Value="13"/> <Setter Property="BorderBrush" Value="Transparent"/> <!-- 这里从5改为0 --> <Setter Property="BorderThickness" Value="0"/> <Setter Property="Margin" Value="0,0,0,0"/> <!-- 覆盖选中状态的单元格边框 --> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="Background" Value="{StaticResource PageBackgroundColor}"/> </Trigger> <DataTrigger Binding="{Binding Level}" Value="Error"> <Setter Property="Foreground" Value="OrangeRed"/> </DataTrigger> <DataTrigger Binding="{Binding Level}" Value="Warning"> <Setter Property="Foreground" Value="Orange"/> </DataTrigger> <DataTrigger Binding="{Binding Level}" Value="Info"> <Setter Property="Foreground" Value="DarkGray"/> </DataTrigger> <DataTrigger Binding="{Binding Level}" Value="Success"> <Setter Property="Foreground" Value="LimeGreen"/> </DataTrigger> </Style.Triggers> </Style> </DataGrid.CellStyle> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding LogTime}" Header="Time" Width="Auto" > <DataGridTextColumn.HeaderStyle> <Style TargetType="{x:Type DataGridColumnHeader}"> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="FontSize" Value="12" /> <Setter Property="Foreground" Value="white" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="Background" Value="{StaticResource PageBackgroundColor}"/> <Setter Property="BorderBrush" Value="Transparent" /> <Setter Property="BorderThickness" Value="0"/> <!-- 隐藏列头分隔线 --> <Setter Property="SeparatorBrush" Value="Transparent"/> </Style> </DataGridTextColumn.HeaderStyle> <DataGridTextColumn.ElementStyle> <Style> <Setter Property="TextBlock.TextWrapping" Value="Wrap" /> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> <DataGridTextColumn Binding="{Binding Log}" Header="Message" Width="*" > <DataGridTextColumn.HeaderStyle> <Style TargetType="{x:Type DataGridColumnHeader}"> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="FontSize" Value="13" /> <Setter Property="Foreground" Value="white" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="BorderBrush" Value="Transparent" /> <Setter Property="Background" Value="{StaticResource PageBackgroundColor}"/> <Setter Property="BorderThickness" Value="0"/> <!-- 隐藏列头分隔线 --> <Setter Property="SeparatorBrush" Value="Transparent"/> </Style> </DataGridTextColumn.HeaderStyle> <DataGridTextColumn.ElementStyle> <Style> <Setter Property="TextBlock.TextWrapping" Value="Wrap" /> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> <!--<DataGridTextColumn Binding="{Binding Level}" Header="Log Type" Width="100" > <DataGridTextColumn.HeaderStyle> <Style TargetType="{x:Type DataGridColumnHeader}"> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="FontSize" Value="15" /> <Setter Property="Foreground" Value="white" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="Background" Value="{StaticResource PageBackgroundColor}"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="SeparatorBrush" Value="Transparent"/> </Style> </DataGridTextColumn.HeaderStyle> <DataGridTextColumn.ElementStyle> <Style> <Setter Property="TextBlock.TextAlignment" Value="Center" /> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn>--> </DataGrid.Columns> </DataGrid>
如果不需要保留DataGrid最外层的灰色边框,直接把DataGrid节点的BorderThickness改为0即可。
内容的提问来源于stack exchange,提问作者mish
相关产品推荐
相关产品推荐

