原文:
WPF DataGrid多表头/列头,多行头,合并单元格,一列占据多行
先上效果图:
思路说明:这是两个DataGrid,没有嵌套,位置和高度保持一致,在加上ScrollViewer滚动条,这就像是在一个DataGrid中。
缺点:
因为最外层有透明的Border,所以没有办法去点击任何一个单元格,也选中不了。
也可以将最层的Border去掉,但是这样一来,鼠标便不能在窗体的任意位置进行滚动。选中单元格的时候不会选择到两个Datagrid的行或单元格。
总体来说,只能用来查询数据,要是有选中单元格进行的操作,就不行了。
XAml:
![](https://i-blog.csdnimg.cn/blog_migrate/5f94c2fecb4ccb2f76f50c965d122407.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/50828477840f9be8f01bb82af691c7d1.gif)
<Window x:Class="PracticeProject.Achievement" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:PracticeProject" mc:Ignorable="d" Title="Achievement" Height="881" Width="1330" Loaded="Window_Loaded"> <Window.Resources> <Style x:Key="MSAll" TargetType="DataGridColumnHeader"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Grid> <!--模板绑定--> <Grid Width="166" Height="74"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <!--设置标题名称--> <Label Content="所有图斑" FontFamily="微软雅黑" FontWeight="Black" FontSize="14" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.ColumnSpan="2" /> <!--设置间隔线 水平中2--> <Rectangle Fill="#ccc" Height="1" Visibility="Visible" VerticalAlignment="Top" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="3" /> <!--设置间隔线 垂直中1--> <Rectangle Fill="#ccc" Width="1" Visibility="Visible" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center"/> <Label Content="个数" FontFamily="微软雅黑" FontSize="14" Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center" /> <!--设置间隔线 垂直右2--> <Rectangle Fill="#ccc" Width="1" Visibility="Visible" Grid.Row="0" Grid.RowSpan="2" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Right"/> <Label Content="面积" FontFamily="微软雅黑" FontSize="14" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" /> <!--设置间隔线 水平下2--> <Rectangle Fill="#ccc" Height="1" Visibility="Visible" VerticalAlignment="Bottom" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="3" /> </Grid> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="CPNMSTrue" TargetType="DataGridColumnHeader"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Grid> <!--模板绑定--> <Grid Width="420" Height="74"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <!--设置标题名称--> <Label FontFamily="微软雅黑" FontSize="14" FontWeight="Black" Content="核查单位已核查图斑" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.ColumnSpan="5" /> <!--设置间隔线--> <Rectangle Fill="#ccc" Height="1" Visibility="Visible" Grid.Row="0" Grid.RowSpan="2" Grid.ColumnSpan="5" /> <Rectangle Fill="#ccc" Width="1" Visibility="Visible" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" /> <Label FontFamily="微软雅黑" FontSize="14" Content="个数" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" /> <Rectangle Fill="#ccc" Width="1" Visibility="Visible" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" /> <Label FontFamily="微软雅黑" FontSize="14" Content="面积" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" /> <Rectangle Fill="#ccc" Width="1" Visibility="Visible" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" /> <Label FontFamily="微软雅黑" FontSize="14" Content="通过个数" Grid.Row="2" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center" /> <Rectangle Fill="#ccc" Width="1" Visibility="Visible" Grid.Row="1" Grid.Column="3" Grid.ColumnSpan="2" /> <Label FontFamily="微软雅黑" FontSize="14" Content="不通过个数" Grid.Row="2" Grid.Column="3" VerticalAlignment="Center" HorizontalAlignment="Center" /> <Rectangle Fill="#ccc" Width="1" Visibility="Visible" Grid.Row="0" Grid.RowSpan="2" Grid.Column="4" HorizontalAlignment="Right" /> <Label FontFamily="微软雅黑" FontSize