WPF入門教程系列二十四——DataGrid使用示例(1)

WPF入門教程系列二——Application介紹

WPF入門教程系列三——Application介紹(續)

WPF入門教程系列四——Dispatcher介紹

WPF入門教程系列五——Window 介紹

WPF入門教程系列十一——依賴屬性(一)

WPF入門教程系列十五——WPF中的數據綁定(一)

 

        WPF技術的主要特點是數據驅動UI,所以在使用WPF技術開發的過程中是以數據爲核心的,WPF提供了數據綁定機制,當數據發生變化時,WPF會自動發出通知去更新UI。

       今天我們來學習.NET 7中的WPF裏面的DataGrid的有關知識。數據表格DataGrid是一個使用非常廣泛的控件,不管是在Asp.Net中的網頁開發還是WinForm(WPF)應用程序開發都會頻繁使用。通過數據表格DataGrid可以靈活、方便、有效的顯示各種數據。自己翻看之前寫的DataGrid的示例,這個示例寫的有些簡單,沒有使用Command指令,沒有使用MVVM模式,現在看來有些欠缺。準備將這個DataGrid示例進行完善一下,並在示例中應用Command指令與MVVM模式。

       WPF控件DataGrid 提供了一種靈活的方法,用於在行和列中顯示數據集合。 DataGrid包括用於託管自定義內容的內置列類型和模板列。內置行類型包括一個下拉詳細信息部分,可用於在單元格值下方顯示其他內容。

一、創建項目

1. 在Visual Studio 2022啓動界面中選擇“創建新項目”,如下圖。

 

2. Visual Studio 2022彈出的“創建新項目”的對話框中做如下選擇。如下圖。

  • 在最左邊的下拉框中,選擇 “C# ,如下圖中1處
  • 在中間的下拉框中,選擇 “所有平臺”,如下圖2處。
  • 在最右邊的下拉框中,選擇“桌面”,如下圖3處。
  • 在下圖中4處,選擇“WPF應用程序”模板,點擊“下一步”按鈕。

    

4.在彈出的“配置新項目”的對話框中,如下圖,在“項目名稱”輸入框中,輸入“WpfGridDemo.NET7”。然後使用鼠標點擊“下一步”按鈕。

 

5. 在彈出的“其他信息”的對話框,如下圖。在“框架”下拉框中,選擇“NET 7.0(標準期限支持)”。其他值選擇默認值即可。然後使用鼠標點擊“創建”按鈕。

 

二、創建實體

       首先進行準備工作,先創建實體,我們使用的是省市縣區的示例數據,這個數據網上比較豐富,可以方便找到。

1. 在Visual Studio 2022的“解決方案資源管理器”中,使用鼠標右鍵單擊“WpfGridDemo.NET7”項目,在彈出菜單中選擇“添加-->新建文件夾”。如下圖。

 

2. 在Visual Studio 2022的“解決方案資源管理器”中,將“新文件夾”改名爲 “Entitys”,然後使用鼠標右鍵單擊“Entitys”文件夾,在彈出菜單中選擇“添加--> 類”。 在“添加新項”對話框中將類命名爲 Area,然後選擇“添加”。

3. 在Visual Studio 2022的“解決方案資源管理器”中,使用鼠標雙擊打開剛纔創建的Area.cs文件,添加如下代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace WpfGridDemo.NET7.Entitys
{
    public class Area
    {
        public int Id { get; set; }

        [StringLength(10)]
        public string Code { get; set; }     

        [StringLength(30)]
       public string Name { get; set; }

 
        [StringLength(10)]

        public string CityCode { get; set; }
        public DateTime Created { get; set; }
        public DateTime Updated { get; set; }

    }
}

 

 4.重得執行第2,3步,在Visual Studio 2022創建City與Province類,這兩個類的代碼如下:

 

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace WpfGridDemo.NET7.Entitys
{
    public class City
    {
        public int Id { get; set; }
        [StringLength(10)]
        public string Code { get; set; }
 
        [StringLength(30)]
        public string Name { get; set; }
 
        [StringLength(10)]
        public string ProvinceCode { get; set; }
    }
}
 


 

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace WpfGridDemo.NET7.Entitys
{
    public class Province
    {
        public int Id { get; set; }
        [StringLength(10)]
        public string Code { get; set; }
 
        [StringLength(30)]
        public string Name { get; set; }

    }

}

5  使用NuGet下載最新版的Entity Framework Core 7。在解決方案資源管理器中——>在項目WpfGridDemo.NET7中的依賴項上鼠標右鍵單擊——>彈出一個菜單,選中“管理NuGet程序包”,如下圖。

 

6. 在打開的NuGet包管理界面的中選擇“瀏覽”標籤頁,在搜索框中輸入“Entity”,找到最新版本Entity Framework Core,點擊安裝。如下圖。

 7.  Visual Studio 2022 開始安裝 EntityFrameworkCore 7.0.3,會彈出安裝確認界面,點擊“OK”按鈕。如下圖。

Microsoft.EntityFrameworkCore

Microsoft.EntityFrameworkCore.SqlServer

8.安裝完成之後,如下圖。

9. 在Visual Studio 2022的解決方案資源管理器中,使用鼠標右鍵點擊“WpfGridDemo.NET7”項目,在彈出菜單中選擇“添加-->類”,在彈出的“添加新項”對話框中,選擇添加 “GridDbContext”類,並在中定義實體對應的DbSet,以應用Code First 數據遷移。添加以下代碼

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using WpfGridDemo.NET7.Entitys;

 
namespace WpfGridDemo.NET7
{
    public class GridDbContext : DbContext
    {
        public GridDbContext(DbContextOptions<GridDbContext> options)
 
               : base(options)
        {
 
        }

        public DbSet<Area> Area { get; set; }
        public DbSet<City> City { get; set; }
        public DbSet<Province> Province { get; set; }
 
    }
}

10.在Visual Studio 2017中的資源管理器中找到appsettings.json文件,用鼠標雙擊打開,在文件中添加一個連接字符串,代碼如下。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add></add>
    </appSettings>
    <connectionStrings>

        <add name="GridDbContext" connectionString="Server=.;Database=EFCoreDemo;Trusted_Connection=True;
Encrypt=False;TrustServerCertificate=True;MultipleActiveResultSets=true"
/> </connectionStrings> </configuration>
11.從Visual Studio 2022的菜單中選擇“工具->NuGet包管理器器—>程序包管理器控制檯”菜單。

  12. 在PMC中,默認項目選擇EntityframeworkCore對應的項目後。輸入以下命令:Add-Migration AddEntityCitys,創建遷移。

  13. 在上面的命令執行完畢之後,創建成功後,會在Migrations文件夾下創建時間_AddEntityCitys格式的類文件,這些代碼是基於DbContext指定的模型。

  14.在程序包管理器控制檯,輸入Update-Database,回車執行遷移。執行成功後,以上三個步驟,我在前面的文章中已經多次寫過了,這裏就簡單寫一下。

 15. 在SQL Server Management Studio中查看數據庫,Province、Area、City三個表創建成功。至於具體的數據,請在網上查找。

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章