WPF DataGrid多表头/列头,多行头,合并单元格,一列占据多行

简介: 原文:WPF DataGrid多表头/列头,多行头,合并单元格,一列占据多行  先上效果图: 思路说明:这是两个DataGrid,没有嵌套,位置和高度保持一致,在加上ScrollViewer滚动条,这就像是在一个DataGrid中。
原文: WPF DataGrid多表头/列头,多行头,合并单元格,一列占据多行

 

先上效果图:

思路说明:这是两个DataGrid,没有嵌套,位置和高度保持一致,在加上ScrollViewer滚动条,这就像是在一个DataGrid中。

缺点:

因为最外层有透明的Border,所以没有办法去点击任何一个单元格,也选中不了。

也可以将最层的Border去掉,但是这样一来,鼠标便不能在窗体的任意位置进行滚动。选中单元格的时候不会选择到两个Datagrid的行或单元格。

总体来说,只能用来查询数据,要是有选中单元格进行的操作,就不行了。

XAml:

<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="14" Content="通过率" Grid.Row="2" Grid.Column="4" VerticalAlignment="Center" HorizontalAlignment="Center" />
                                <Rectangle Fill="#ccc" Height="1" Visibility="Visible" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="5" VerticalAlignment="Bottom"/>
                            </Grid>

                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <!--ScrollChanged="ScrollViewer_ScrollChanged"-->
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <Grid  Width="1305" MinHeight="881">

            <Border Width="1296" Height="60" HorizontalAlignment="Left" VerticalAlignment="Top">
                <Label Content="工作成果统计" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontFamily="微软雅黑" FontSize="18"></Label>
            </Border>

            <!--#region datagrid1-->
            <DataGrid Name="dategrid1" Width="156" HorizontalAlignment="Left" GridLinesVisibility="None" Background="#fff" BorderBrush="Transparent" AutoGenerateColumns="False" CanUserAddRows="False" IsReadOnly="True" SelectionMode="Single" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" Margin="0,60,0,0" Panel.ZIndex="5">

                <!--#region 表头-->
                <DataGrid.Style>
                    <Style TargetType="DataGrid">
                        <Setter Property="Background" Value="#E7F3FF"></Setter>
                        <Setter Property="HeadersVisibility" Value="Column"></Setter>
                    </Style>
                </DataGrid.Style>
                <!--#endregion-->

                <!--#region 数据网格的列标题样式属性-->
                <DataGrid.ColumnHeaderStyle>
                    <!--样式类型:DataGridColumnHeader(数据网格列标题)-->
                    <Style TargetType="{x:Type DataGridColumnHeader}">
                        <Setter Property="Height" Value="74"/>
                        <Setter Property="Background" Value="#E1F0FF"/>
                        <Setter Property="HorizontalContentAlignment" Value="Center"/>
                        <Setter Property="FontFamily" Value="微软雅黑"></Setter>
                        <Setter Property="FontSize" Value="14"></Setter>
                        <Setter Property="BorderBrush" Value="#ccc"></Setter>
                        <Setter Property="BorderThickness">
                            <Setter.Value>
                                <Thickness Top="1" Right="1" Bottom="1"></Thickness>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="FontWeight" Value="Black"></Setter>
                    </Style>
                </DataGrid.ColumnHeaderStyle>
                <!--#endregion-->

                <DataGrid.RowStyle>
                    <Style TargetType="DataGridRow">
                        <Setter Property="Height" Value="259" />
                        <Setter Property="BorderThickness">
                            <Setter.Value>
                                <Thickness Right="2"></Thickness>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="BorderBrush" Value="#ccc"></Setter>
                        <Style.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Foreground" Value="Red"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.RowStyle>

                <!--#region DataGrid的CellStyle属性(数据表格的单元格样式-->
                <DataGrid.CellStyle >
                    <Style TargetType="DataGridCell">
                        <Setter Property="BorderThickness">
                            <Setter.Value>
                                <Thickness Bottom="1" Right="1"></Thickness>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="BorderBrush" Value="#ccc"></Setter>
                        <!--样式的触发器-->
                        <Style.Triggers>
                            <!--是否选中是触发-->
                            <Trigger Property="IsSelected" Value="True">
                                <!--触发:背景-->
                                <Setter Property="Background">
                                    <!--设置触发背景的值-->
                                    <Setter.Value>
                                        <!--Color:颜色,Opacity:透明度-->
                                        <SolidColorBrush Color="AntiqueWhite" Opacity="0.9"/>
                                    </Setter.Value>
                                </Setter>
                                <!--设置字体颜色(Foreground):红色(Red)-->
                                <Setter Property="Foreground" Value="DarkMagenta"></Setter>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.CellStyle>
                <!--#endregion-->

                <DataGrid.Columns>

                    <!--#region 行政区代码-->
                    <DataGridTemplateColumn x:Name="DistrictCode" Header="行政区代码" Width="*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal">
                                    <Label Content="{Binding Code}" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"></Label>
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <!--#endregion-->

                    <!--#region 行政区名称-->
                    <DataGridTemplateColumn x:Name="DistrictName" Header="行政区名称" Width="*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal">
                                    <Label Content="{Binding Name}" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"></Label>
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <!--#endregion-->

                </DataGrid.Columns>

            </DataGrid>
            <!--#endregion-->

            <!--#region datagrid2-->
            <DataGrid Width="1140" Name="dategrid2" HorizontalAlignment="Left" Margin="154,60,0,0" GridLinesVisibility="None" Background="#fff" BorderBrush="Transparent" AutoGenerateColumns="False" CanUserAddRows="False" IsReadOnly="True" SelectionMode="Single" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden">

                <!--#region 表头-->
                <DataGrid.Style>
                    <Style TargetType="DataGrid">
                        <Setter Property="Background" Value="#E7F3FF"></Setter>
                        <Setter Property="HeadersVisibility" Value="Column"></Setter>
                    </Style>
                </DataGrid.Style>
                <!--#endregion-->

                <!--#region 数据网格的列标题样式属性-->
                <DataGrid.ColumnHeaderStyle>
                    <!--样式类型:DataGridColumnHeader(数据网格列标题)-->
                    <Style TargetType="{x:Type DataGridColumnHeader}">
                        <Setter Property="Height" Value="74"/>
                        <Setter Property="BorderThickness">
                            <Setter.Value>
                                <Thickness Left="1" Top="1" Right="1" Bottom="1"></Thickness>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Background" Value="#E1F0FF"/>
                        <Setter Property="BorderBrush" Value="#ccc"></Setter>
                        <Setter Property="HorizontalContentAlignment" Value="Center"/>
                        <Setter Property="FontFamily" Value="微软雅黑"></Setter>
                        <Setter Property="FontWeight" Value="Black"></Setter>
                        <Setter Property="FontSize" Value="14"></Setter>
                    </Style>
                </DataGrid.ColumnHeaderStyle>
                <!--#endregion-->

                <DataGrid.RowStyle>
                    <Style TargetType="DataGridRow">
                        <Setter Property="Height" Value="37" />
                        <Setter Property="BorderBrush" Value="#ccc"></Setter>
                        <Setter Property="BorderThickness">
                            <Setter.Value>
                                <Thickness></Thickness>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGrid.RowStyle>

                <!--#region DataGrid的CellStyle属性(数据表格的单元格样式-->
                <DataGrid.CellStyle >
                    <Style TargetType="DataGridCell">
                        <Setter Property="BorderThickness">
                            <Setter.Value>
                                <Thickness Bottom="1" Right="1"></Thickness>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="BorderBrush" Value="#ccc"></Setter>
                        <!--样式的触发器-->
                        <Style.Triggers>
                            <!--是否选中是触发-->
                            <Trigger Property="IsSelected" Value="True">
                                <!--触发:背景-->
                                <Setter Property="Background">
                                    <!--设置触发背景的值-->
                                    <Setter.Value>
                                        <!--Color:颜色,Opacity:透明度-->
                                        <SolidColorBrush Color="AntiqueWhite" Opacity="0.9"/>
                                    </Setter.Value>
                                </Setter>
                                <!--设置字体颜色(Foreground):红色(Red)-->
                                <Setter Property="Foreground" Value="DarkMagenta"></Setter>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.CellStyle>
                <!--#endregion-->

                <DataGrid.Columns>

                    <!--#region 图斑类型方法2:放入datagrid-->
                    <!--<DataGridTemplateColumn x:Name="datacol" Header="图斑类型" Width="130">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>

                            <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal">
                                <Label Content="{Binding DistrictModels.Code}" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"></Label>
                            </StackPanel>

                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>-->
                    <!--#endregion-->

                    <!--#region 图斑类型-->
                    <DataGridTemplateColumn x:Name="map_spotType" Header="图斑类型" Width="130">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>

                                <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal">
                                    <Label Content="{Binding Name}" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"></Label>
                                </StackPanel>

                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <!--#endregion-->

                    <!--#region 所有图斑-->
                    <DataGridTemplateColumn Header="sec" HeaderStyle="{StaticResource MSAll}">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>

                                <!--数据内容-->
                                <StackPanel Orientation="Horizontal">
                                    <Label Content="{Binding AllNumber}" Width="82" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                                    <Rectangle Fill="#ccc" VerticalAlignment="Stretch" Margin="0" Width="1" />
                                    <Label Content="{Binding AllArea}" Width="80" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                                </StackPanel>

                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <!--#endregion-->

                    <!--#region 核查单位已核查图斑-->
                    <DataGridTemplateColumn Header="sec" HeaderStyle="{StaticResource CPNMSTrue}">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>

                                <!--数据内容-->
                                <StackPanel Orientation="Horizontal">
                                    <!--个数-->
                                    <Label Content="{Binding HCTureNumber}" Width="83" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                                    <Rectangle Fill="#ccc" VerticalAlignment="Stretch" Margin="0" Width="1" />
                                    <!--面积-->
                                    <Label Content="{Binding HCTrueArea}" Width="83" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                                    <Rectangle Fill="#ccc" VerticalAlignment="Stretch" Margin="0" Width="1" />
                                    <!--通过个数-->
                                    <Label Content="{Binding HCTruePassNumber}" Width="83" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                                    <Rectangle Fill="#ccc" VerticalAlignment="Stretch" Margin="0" Width="1" />
                                    <!--不通过个数-->
                                    <Label Content="{Binding HCTrueNoPassNumber}" Width="83" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                                    <Rectangle Fill="#ccc" VerticalAlignment="Stretch" Margin="0" Width="1" />
                                    <!--通过率-->
                                    <Label Content="{Binding HCTruePassingRate}" Width="80" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                                </StackPanel>

                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <!--#endregion-->

                    <!--#region 监理单位已核查图斑-->
                    <DataGridTemplateColumn Header="sec" HeaderStyle="{StaticResource CPNMSTrue}">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>

                                <!--数据内容-->
                                <StackPanel Orientation="Horizontal">
                                    <!--个数-->
                                    <Label Content="{Binding JLTureNumber}" Width="83" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                                    <Rectangle Fill="#ccc" VerticalAlignment="Stretch" Margin="0" Width="1" />
                                    <!--面积-->
                                    <Label Content="{Binding JLTrueArea}" Width="83" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                                    <Rectangle Fill="#ccc" VerticalAlignment="Stretch" Margin="0" Width="1" />
                                    <!--通过个数-->
                                    <Label Content="{Binding JLTruePassNumber}" Width="83" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                                    <Rectangle Fill="#ccc" VerticalAlignment="Stretch" Margin="0" Width="1" />
                                    <!--不通过个数-->
                                    <Label Content="{Binding JLTrueNoPassNumber}" Width="83" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                                    <Rectangle Fill="#ccc" VerticalAlignment="Stretch" Margin="0" Width="1" />
                                    <!--通过率-->
                                    <Label Content="{Binding JLTruePassingRate}" Width="80" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
                                </StackPanel>

                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <!--#endregion-->

                </DataGrid.Columns>

            </DataGrid>
            <!--#endregion-->

            <Rectangle Canvas.Left ="20" Canvas.Top ="30" Grid.Row="0" Grid.Column="0" Panel.ZIndex="1" Opacity="0" Fill ="White" Stroke ="Black" x:Name ="mainrectangle" />
        </Grid>
    </ScrollViewer>

</Window>
View Code

后台代码:

using PracticeProject.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PracticeProject
{
    /// <summary>
    /// Achievement.xaml 的交互逻辑
    /// </summary>
    public partial class Achievement : Window
    {
        public Achievement()
        {
            InitializeComponent();
        }
        List<DistrictModel> dmList = new List<Model.DistrictModel>();
        List<MapSpotModel> MSList = new List<Model.MapSpotModel>();
        public void SelectMS()
        {
            #region 构造行政区数据
            string[] str1 = { "XX县", "ZZ县", "CC县", "VV县" };
            string[] str2 = { "QQQ", "WWW", "AAA", "SSS", "EEE", "DDD", "RRR" };
            #endregion 构造行政区数据_end
            //随机数
            Random rd0 = new Random();
            for (int i = 0; i < str1.Count(); i++)
            {
                int cd = 101 + i;
                dmList.Add(new DistrictModel() { Id = i + 1, Code = cd, Name = str1[i] });
                #region 构造图斑数据
                for (int j = 0; j < str2.Count(); j++)
                {
                    #region 生成随机图斑及面积个数并计算其值

                    #region 所有图斑个数
                    int AllNumber1 = rd0.Next(1, 3000);
                    #endregion 所有图斑个数_end

                    #region 核查单位已核查图斑个数
                    //总共
                    int HCTureNumber1 = rd0.Next(1, AllNumber1);
                    //已核查通过个数
                    int HCTruePassNumber1 = rd0.Next(1, HCTureNumber1);
                    //已核查不通过个数
                    int HCTrueNoPassNumber1 = HCTureNumber1 - HCTruePassNumber1;
                    //通过率
                    string HCTruePassingRate1 = (Math.Round(((double)HCTruePassNumber1 / HCTureNumber1) * 100)) + "%";
                    #endregion 核查单位已核查图斑个数_end

                    #region 核查单位已核查图斑个数
                    //总共
                    int JLTureNumber1 = rd0.Next(1, AllNumber1);
                    //已核查通过个数
                    int JLTruePassNumber1 = rd0.Next(1, JLTureNumber1);
                    //已核查不通过个数
                    int JLTrueNoPassNumber = JLTureNumber1 - JLTruePassNumber1;
                    //通过率
                    string JLTruePassingRate1 = (Math.Round(((double)JLTruePassNumber1 / JLTureNumber1) * 100)) + "%";
                    #endregion 核查单位已核查图斑个数_end

                    #region 所有图斑面积
                    double AllArea1 = Math.Round((rd0.NextDouble() * 1000), 2);
                    double HCTrueArea1 = rd0.Next(1, (int)AllArea1);
                    double JLTrueArea1 = rd0.Next(1, (int)AllArea1);
                    #endregion

                    #endregion 
                    MSList.Add(new MapSpotModel()
                    {
                        Id = i + 1,
                        Name = str2[j],
                        Code = cd,
                        AllNumber = AllNumber1,
                        AllArea = AllArea1,
                        HCTureNumber = HCTureNumber1,
                        HCTrueArea = HCTrueArea1,
                        HCTruePassNumber = HCTruePassNumber1,
                        HCTrueNoPassNumber = HCTrueNoPassNumber1,
                        HCTruePassingRate = HCTruePassingRate1,
                        JLTureNumber = JLTureNumber1,
                        JLTrueArea = JLTrueArea1,
                        JLTruePassNumber = JLTruePassNumber1,
                        JLTrueNoPassNumber = JLTrueNoPassNumber,
                        JLTruePassingRate = JLTruePassingRate1,
                        DistrictModels = new DistrictModel() { Id = i, Code = cd, Name = str2[j] }
                    });
                }
                #endregion 构造图斑数据_end
            }
            dategrid1.ItemsSource = dmList;
            dategrid2.ItemsSource = MSList;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SelectMS();
        }


    }
}
View Code

第一个Model:

/// <summary>
    /// 行政区表
    /// </summary>
    public class DistrictModel
    {
        /// <summary>
        /// 主键
        /// </summary>
        private int id;
        /// <summary>
        /// 代码
        /// </summary>
        private int code;
        /// <summary>
        /// 名称
        /// </summary>
        private string name;

        public int Id
        {
            get
            {
                return id;
            }

            set
            {
                id = value;
            }
        }

        /// <summary>
        /// 代码
        /// </summary>
        public int Code
        {
            get
            {
                return code;
            }

            set
            {
                code = value;
            }
        }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
            }
        }

    }
View Code

第二个Model:

 /// <summary>
    ////// </summary>
    public class MapSpotModel
    {
        private int id;
        //名称
        private string name;
        //代码
        private int code;
        //个数
        private int allNumber;
        //面积
        private double allArea;

        //个数
        private int hCTureNumber;
        //面积
        private double hCTrueArea;
        //通过个数
        private int hCTruePassNumber;
        //不通过个数
        private int hCTrueNoPassNumber;
        //通过率
        private string hCTruePassingRate;

        //个数
        private int jLTureNumber;
        //面积
        private double jLTrueArea;
        //通过个数
        private int jLTruePassNumber;
        //不通过个数
        private int jLTrueNoPassNumber;
        //通过率
        private string jLTruePassingRate;

        private DistrictModel districtModels;
        public int Id
        {
            get
            {
                return id;
            }

            set
            {
                id = value;
            }
        }
        /// <summary>
        /// 名称
        /// </summary>
        public string Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
            }
        }

        /// <summary>
        /// 代码
        /// </summary>
        public int Code
        {
            get
            {
                return code;
            }

            set
            {
                code = value;
            }
        }

        /// <summary>
        /// 个数
        /// </summary>
        public int AllNumber
        {
            get
            {
                return allNumber;
            }

            set
            {
                allNumber = value;
            }
        }

        /// <summary>
        /// 面积
        /// </summary>
        public double AllArea
        {
            get
            {
                return allArea;
            }

            set
            {
                allArea = value;
            }
        }

        /// <summary>
        /// 个数
        /// </summary>
        public int HCTureNumber
        {
            get
            {
                return hCTureNumber;
            }

            set
            {
                hCTureNumber = value;
            }
        }

        /// <summary>
        /// 面积
        /// </summary>
        public double HCTrueArea
        {
            get
            {
                return hCTrueArea;
            }

            set
            {
                hCTrueArea = value;
            }
        }

        /// <summary>
        /// 通过个数
        /// </summary>
        public int HCTruePassNumber
        {
            get
            {
                return hCTruePassNumber;
            }

            set
            {
                hCTruePassNumber = value;
            }
        }

        /// <summary>
        /// 不通过个数
        /// </summary>
        public int HCTrueNoPassNumber
        {
            get
            {
                return hCTrueNoPassNumber;
            }

            set
            {
                hCTrueNoPassNumber = value;
            }
        }

        /// <summary>
        /// 通过率
        /// </summary>
        public string HCTruePassingRate
        {
            get
            {
                return hCTruePassingRate;
            }

            set
            {
                hCTruePassingRate = value;
            }
        }

        /// <summary>
        /// 个数
        /// </summary>
        public int JLTureNumber
        {
            get
            {
                return jLTureNumber;
            }

            set
            {
                jLTureNumber = value;
            }
        }

        /// <summary>
        /// 面积
        /// </summary>
        public double JLTrueArea
        {
            get
            {
                return jLTrueArea;
            }

            set
            {
                jLTrueArea = value;
            }
        }

        /// <summary>
        /// 通过个数
        /// </summary>
        public int JLTruePassNumber
        {
            get
            {
                return jLTruePassNumber;
            }

            set
            {
                jLTruePassNumber = value;
            }
        }

        /// <summary>
        /// 不通过个数
        /// </summary>
        public int JLTrueNoPassNumber
        {
            get
            {
                return jLTrueNoPassNumber;
            }

            set
            {
                jLTrueNoPassNumber = value;
            }
        }

        /// <summary>
        /// 监理单位已审核图斑-通过率
        /// </summary>
        public string JLTruePassingRate
        {
            get
            {
                return jLTruePassingRate;
            }

            set
            {
                jLTruePassingRate = value;
            }
        }

        public DistrictModel DistrictModels
        {
            get
            {
                return districtModels;
            }

            set
            {
                districtModels = value;
            }
        }
    }
View Code

 

目录
相关文章
|
4月前
|
开发框架 前端开发 JavaScript
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(10) -- 在DataGrid上直接编辑保存数据
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(10) -- 在DataGrid上直接编辑保存数据
|
4月前
|
开发框架 前端开发 搜索推荐
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(4) -- 实现DataGrid数据的导入和导出操作
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(4) -- 实现DataGrid数据的导入和导出操作
|
4月前
|
开发框架 前端开发 JavaScript
在WPF应用中实现DataGrid的分组显示,以及嵌套明细展示效果
在WPF应用中实现DataGrid的分组显示,以及嵌套明细展示效果
在WPF应用中实现DataGrid的分组显示,以及嵌套明细展示效果
|
4月前
|
前端开发 测试技术 C#
WPF/C#:在DataGrid中显示选择框
WPF/C#:在DataGrid中显示选择框
65 0
|
数据挖掘 数据处理 C#
WPF技术之DataGrid控件
WPF DataGrid是一种可以显示和编辑数据的界面控件。它可以作为表格形式展示数据,支持添加、删除、修改、排序和分组操作。
313 0
|
7月前
|
前端开发 C#
浅谈WPF之DataGrid动态生成列
在日常开发中,DataGrid作为二维表格,非常适合数据的展示和统计。通常情况下,一般都有固定的格式和确定的数据列展示,但是在某些特殊情况下,也可能会需要用到动态生成列。本文以一些简单的小例子,简述在WPF开发中,如何动态生成DataGrid的行和列,仅供学习分享使用,如有不足之处,还请指正。
348 2
|
前端开发 C# 数据库
让WPF中的DataGrid像Excel一样可以筛选(上)
让WPF中的DataGrid像Excel一样可以筛选(上)
208 0
让WPF中的DataGrid像Excel一样可以筛选(上)
让WPF中的DataGrid像Excel一样可以筛选(下)
让WPF中的DataGrid像Excel一样可以筛选(下)
235 0
|
C# 数据库
WPF中DataGrid控件绑定数据源
WPF中DataGrid控件绑定数据源
179 0
WPF 点击 Datagrid 中的TextBox 控件获取其所在行的数据
WPF 点击 Datagrid 中的TextBox 控件获取其所在行的数据