金喜正规买球

DevExpress WPF中文教程:如何将WPF数据网格绑定到实体框架核心源?

翻译|使用教程|编辑:龚雪|2025-09-11 10:21:20.480|阅读 12 次

概述:本文主要介绍如何使用DevExpress WPF Grid控件将数据网格绑定到实体框架核心源,欢迎下载最新版组件体验!

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

相关链接:

DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品,还是以数据为中心的商业智能产品,都能通过DevExpress WPF控件来实现。

本文档展示了如何将GridControl绑定到实体框架(EF)核心源。

获取DevExpress WPF v25.1正式版下载

DevExpress技术交流群11:749942875      欢迎一起进群讨论

要求
  • 或更高版本
安装Entity Framework Core(实体框架核心)

跳转到Tools | NuGet Package Manager | Manage NuGet Packages for Solution

在“Browse”选项卡中,搜索 ‘microsoft sqlserver’ 关键字,并为当前项目安装Microsoft.EntityFrameworkCore.Sqlite包,选择与应用程序所针对的.NET版本兼容的包版本,接受许可协议。

MyEclipse中文使用教程图集
创建数据上下文

出于本教程的目的,使用Demo Center中包含的Countries.db数据库。

创建Data文件夹,并从以下文件夹中添加Countriesdb数据库:C:\Users\Public\Public Documents\DevExpress Demos 25.1\Components\Data

 下面的代码片段演示了Countries表的数据模型,Key属性指定标识Country实体的属性。

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表创建要给数据上下文,从派生数据上下文类,并为数据集合公开属性。

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();
}
}

重新生成项目来编译生成的类。

将GridControl绑定到Data

将GridControl添加到项目中。

打开GridControl的并调用。

MyEclipse中文使用教程图集

1. 选择 CountriesContext源。

MyEclipse中文使用教程图集

2. 选择CountryObject表。

MyEclipse中文使用教程图集

3. 您可以选择任何数据绑定模型,出于本教程的目的,选择适合大型数据库的 Instant Feedback Mode

MyEclipse中文使用教程图集

4. 确保Key Propery选项设置为Id

MyEclipse中文使用教程图集

5. 选择View Model将代码添加到视图模型中。

MyEclipse中文使用教程图集

单击Select a Data Context,选择ViewModel类并单击OK。

MyEclipse中文使用教程图集

启用Set selected class as the data context选项并单击Finish

MyEclipse中文使用教程图集

Items Source Wizard(项目源向导)在ViewModel中生成数据绑定代码,并在XAML中指定数据上下文和GridControl选项:

XAML

<dx:ThemedWindow
xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
xmlns:dx="//schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxg="//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;
}
}
}

运行应用程序。

MyEclipse中文使用教程图集

更多产品资讯及授权,欢迎来电咨询:023-68661681


关于慧都科技

慧都是⼀家⾏业数字化解决⽅案公司,专注于软件、⽯油与⼯业领域,以深⼊的业务理解和⾏业经验,帮助企业实现智能化转型与持续竞争优势。

 慧都是DevExpress的中国区的合作伙伴,DevExpress作为用户界面领域的优秀产品,帮助企业高效构建权限管理、数据可视化(如网格/图表/仪表盘)、跨平台系统(WinForms/ASP.NET/.NET MAUI)及行业定制解决方案,加速开发并强化交互体验。


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@fc6vip.cn

文章转载自:慧都网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP