翻譯|使用教程|編輯:龔雪|2025-09-11 10:21:20.480|閱讀 61 次
概述:本文主要介紹如何使用DevExpress WPF Grid控件將數(shù)據(jù)網(wǎng)格綁定到實(shí)體框架核心源,歡迎下載最新版組件體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
DevExpress WPF擁有120+個(gè)控件和庫,將幫助您交付滿足甚至超出企業(yè)需求的高性能業(yè)務(wù)應(yīng)用程序。通過DevExpress WPF能創(chuàng)建有著強(qiáng)大互動(dòng)功能的XAML基礎(chǔ)應(yīng)用程序,這些應(yīng)用程序?qū)W⒂诋?dāng)代客戶的需求和構(gòu)建未來新一代支持觸摸的解決方案。 無論是Office辦公軟件的衍伸產(chǎn)品,還是以數(shù)據(jù)為中心的商業(yè)智能產(chǎn)品,都能通過DevExpress WPF控件來實(shí)現(xiàn)。
本文檔展示了如何將GridControl綁定到實(shí)體框架(EF)核心源。
DevExpress技術(shù)交流群11:749942875 歡迎一起進(jìn)群討論
跳轉(zhuǎn)到Tools | NuGet Package Manager | Manage NuGet Packages for Solution。
在“Browse”選項(xiàng)卡中,搜索 ‘microsoft sqlserver’ 關(guān)鍵字,并為當(dāng)前項(xiàng)目安裝Microsoft.EntityFrameworkCore.Sqlite包,選擇與應(yīng)用程序所針對的.NET版本兼容的包版本,接受許可協(xié)議。
出于本教程的目的,使用Demo Center中包含的Countries.db數(shù)據(jù)庫。
創(chuàng)建Data文件夾,并從以下文件夾中添加Countriesdb數(shù)據(jù)庫:C:\Users\Public\Public Documents\DevExpress Demos 25.1\Components\Data
下面的代碼片段演示了Countries表的數(shù)據(jù)模型,Key屬性指定標(biāo)識Country實(shí)體的屬性。
C#
using System; using System.ComponentModel.DataAnnotations; public class CountryObject { [Key] public int Id { get; set; } public string Country { get; set; } public string Currency { get; set; } public string Capital { get; set; } public int Population { get; set; } public string Languages { get; set; } }
為Countries表創(chuàng)建要給數(shù)據(jù)上下文,從派生數(shù)據(jù)上下文類,并為數(shù)據(jù)集合公開屬性。
C#
using Microsoft.EntityFrameworkCore; public partial class CountriesContext : DbContext { public CountriesContext() : base() { } protected override void OnConfiguring( DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite( "Data Source=file:Data/Countries.db"); base.OnConfiguring(optionsBuilder); } public virtual DbSet<CountryObject> Countries { get; set; } }
從類派生視圖模型:
C#
using DevExpress.Mvvm; using System.Collections.Generic; public class ViewModel : ViewModelBase { CountriesContext countriesContext; public ICollection<CountryObject> Countries { get => GetValue<ICollection<CountryObject>>(); private set => SetValue(value); } public ViewModel() { countriesContext = new CountriesContext(); } }
重新生成項(xiàng)目來編譯生成的類。
將GridControl添加到項(xiàng)目中。
打開GridControl的并調(diào)用。
1. 選擇 CountriesContext源。
2. 選擇CountryObject表。
3. 您可以選擇任何數(shù)據(jù)綁定模型,出于本教程的目的,選擇適合大型數(shù)據(jù)庫的 Instant Feedback Mode。
4. 確保Key Propery選項(xiàng)設(shè)置為Id。
5. 選擇View Model將代碼添加到視圖模型中。
單擊Select a Data Context,選擇ViewModel類并單擊OK。
啟用Set selected class as the data context選項(xiàng)并單擊Finish。
Items Source Wizard(項(xiàng)目源向?qū)В┰?strong>ViewModel中生成數(shù)據(jù)綁定代碼,并在XAML中指定數(shù)據(jù)上下文和GridControl選項(xiàng):
XAML
<dx:ThemedWindow xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" xmlns:local="clr-namespace:EntityFrameworkCore" x:Class="EntityFrameworkCore.MainWindow" Title="MainWindow" Height="800" Width="1000"> <dx:ThemedWindow.DataContext> <local:ViewModel/> </dx:ThemedWindow.DataContext> <Grid> <dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" ItemsSource="{Binding ItemsSource}"> <dxg:GridControl.TotalSummary> <dxg:GridSummaryItem Alignment="Right" SummaryType="Count"/> </dxg:GridControl.TotalSummary> <dxg:GridControl.View> <dxg:TableView ShowFixedTotalSummary="True"/> </dxg:GridControl.View> <dxg:GridColumn FieldName="Id" IsSmart="True" ReadOnly="True"/> <dxg:GridColumn FieldName="Country" IsSmart="True"/> <dxg:GridColumn FieldName="Currency" IsSmart="True"/> <dxg:GridColumn FieldName="Capital" IsSmart="True"/> <dxg:GridColumn FieldName="Population" IsSmart="True"/> <dxg:GridColumn FieldName="Languages" IsSmart="True"/> </dxg:GridControl> </Grid> </dx:ThemedWindow>
C#
public class ViewModel : ViewModelBase { CountriesContext countriesContext; public ICollection<CountryObject> Countries { get => GetValue<ICollection<CountryObject>>(); private set => SetValue(value); } public ViewModel() { countriesContext = new CountriesContext(); } EntityInstantFeedbackSource _ItemsSource; public EntityInstantFeedbackSource ItemsSource { get { if (_ItemsSource == null) { _ItemsSource = new EntityInstantFeedbackSource { KeyExpression = nameof(CountryObject.Id) }; _ItemsSource.GetQueryable += (sender, e) => { var context = new CountriesContext(); e.QueryableSource = context.Countries.AsNoTracking(); }; } return _ItemsSource; } } }
運(yùn)行應(yīng)用程序。
更多產(chǎn)品資訊及授權(quán),歡迎來電咨詢:023-68661681
慧都是?家?業(yè)數(shù)字化解決?案公司,專注于軟件、?油與?業(yè)領(lǐng)域,以深?的業(yè)務(wù)理解和?業(yè)經(jīng)驗(yàn),幫助企業(yè)實(shí)現(xiàn)智能化轉(zhuǎn)型與持續(xù)競爭優(yōu)勢。
慧都是DevExpress的中國區(qū)的合作伙伴,DevExpress作為用戶界面領(lǐng)域的優(yōu)秀產(chǎn)品,幫助企業(yè)高效構(gòu)建權(quán)限管理、數(shù)據(jù)可視化(如網(wǎng)格/圖表/儀表盤)、跨平臺系統(tǒng)(WinForms/ASP.NET/.NET MAUI)及行業(yè)定制解決方案,加速開發(fā)并強(qiáng)化交互體驗(yàn)。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自:慧都網(wǎng)