DevExpress WPF中文教程:如何将WPF数据网格绑定到本地集合?

翻译|使用教程|编辑:龚雪|2025-08-26 10:02:27.877|阅读 5 次

概述:本文主要介绍DevExpress WPF Grid控件如何将数据绑定到本地集合,欢迎下载最新版组件体验!

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

相关链接:

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

MyEclipse中文使用教程图集

本文主要演示如何将DevExpress WPF GridControl绑定到本地集合,欢迎下载最新版组件体验!

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

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

新增一个数据模型

用Customer类的集合创建一个数据模型:

DataModel.cs

using System;
using System.Collections.ObjectModel;

namespace BindToLocalCollection {
public class Customer {
public string Name { get; set; }
public string City { get; set; }
public int Visits { get; set; }
public DateTime? Birthday { get; set; }
}
public class CustomerDataModel {
public ObservableCollection<Customer> GetCustomers() {
ObservableCollection<Customer> people = new ObservableCollection<Customer>();
people.Add(new Customer() { Name = "Gregory S. Price", City = "Hong Kong", Visits = 4, Birthday = new DateTime(1980, 1, 1) });
people.Add(new Customer() { Name = "Irma R. Marshall", City = "Madrid", Visits = 2, Birthday = new DateTime(1966, 4, 15) });
people.Add(new Customer() { Name = "John C. Powell", City = "Los Angeles", Visits = 6, Birthday = new DateTime(1982, 3, 11) });
people.Add(new Customer() { Name = "Christian P. Laclair", City = "London", Visits = 11, Birthday = new DateTime(1977, 12, 5) });
people.Add(new Customer() { Name = "Karen J. Kelly", City = "Hong Kong", Visits = 8, Birthday = new DateTime(1956, 9, 5) });
people.Add(new Customer() { Name = "Brian C. Cowling", City = "Los Angeles", Visits = 5, Birthday = new DateTime(1990, 2, 27) });
people.Add(new Customer() { Name = "Thomas C. Dawson", City = "Madrid", Visits = 21, Birthday = new DateTime(1965, 5, 5) });
people.Add(new Customer() { Name = "Angel M. Wilson", City = "Los Angeles", Visits = 8, Birthday = new DateTime(1987, 11, 9) });
people.Add(new Customer() { Name = "Winston C. Smith", City = "London", Visits = 1, Birthday = new DateTime(1949, 6, 18) });
people.Add(new Customer() { Name = "Harold S. Brandes", City = "Bangkok", Visits = 3, Birthday = new DateTime(1989, 1, 8) });
people.Add(new Customer() { Name = "Michael S. Blevins", City = "Hong Kong", Visits = 4, Birthday = new DateTime(1972, 9, 14) });
people.Add(new Customer() { Name = "Jan K. Sisk", City = "Bangkok", Visits = 6, Birthday = new DateTime(1989, 5, 7) });
people.Add(new Customer() { Name = "Sidney L. Holder", City = "London", Visits = 19, Birthday = new DateTime(1971, 10, 3) });
return people;
}
}
}

DataModel.vb

Imports System
Imports System.Collections.ObjectModel

Namespace BindToLocalCollection
Public Class Customer
Public Property Name As String
Public Property City As String
Public Property Visits As Integer
Public Property Birthday As DateTime?
End Class

Public Class CustomerDataModel
Public Function GetCustomers() As ObservableCollection(Of Customer)
Dim people As ObservableCollection(Of Customer) = New ObservableCollection(Of Customer)()
people.Add(New Customer() With { .Name = "Gregory S. Price", .City = "Hong Kong", .Visits = 4, .Birthday = New DateTime(1980, 1, 1) })
people.Add(New Customer() With { .Name = "Irma R. Marshall", .City = "Madrid", .Visits = 2, .Birthday = New DateTime(1966, 4, 15) })
people.Add(New Customer() With { .Name = "John C. Powell", .City = "Los Angeles", .Visits = 6, .Birthday = New DateTime(1982, 3, 11) })
people.Add(New Customer() With { .Name = "Christian P. Laclair", .City = "London", .Visits = 11, .Birthday = New DateTime(1977, 12, 5) })
people.Add(New Customer() With { .Name = "Karen J. Kelly", .City = "Hong Kong", .Visits = 8, .Birthday = New DateTime(1956, 9, 5) })
people.Add(New Customer() With { .Name = "Brian C. Cowling", .City = "Los Angeles", .Visits = 5, .Birthday = New DateTime(1990, 2, 27) })
people.Add(New Customer() With { .Name = "Thomas C. Dawson", .City = "Madrid", .Visits = 21, .Birthday = New DateTime(1965, 5, 5) })
people.Add(New Customer() With { .Name = "Angel M. Wilson", .City = "Los Angeles", .Visits = 8, .Birthday = New DateTime(1987, 11, 9) })
people.Add(New Customer() With { .Name = "Winston C. Smith", .City = "London", .Visits = 1, .Birthday = New DateTime(1949, 6, 18) })
people.Add(New Customer() With { .Name = "Harold S. Brandes", .City = "Bangkok", .Visits = 3, .Birthday = New DateTime(1989, 1, 8) })
people.Add(New Customer() With { .Name = "Michael S. Blevins", .City = "Hong Kong", .Visits = 4, .Birthday = New DateTime(1972, 9, 14) })
people.Add(New Customer() With { .Name = "Jan K. Sisk", .City = "Bangkok", .Visits = 6, .Birthday = New DateTime(1989, 5, 7) })
people.Add(New Customer() With { .Name = "Sidney L. Holder", .City = "London", .Visits = 19, .Birthday = New DateTime(1971, 10, 3) })
Return people
End Function
End Class
End Namespace
新增一个视图模型

创建一个从CustomerDataModel接收数据的视图模型类:

ViewModel.cs

using DevExpress.Mvvm;
using System.Collections.ObjectModel;

namespace BindToLocalCollection {
public class ViewModel : ViewModelBase {
public ViewModel() {
Source = CustomerDataModel.GetCustomers();
}
public ObservableCollection<Customer> Source { get; }
}
}

ViewModel.vb

Imports DevExpress.Mvvm
Imports System.Collections.ObjectModel

Namespace BindToLocalCollection
Public Class ViewModel
Inherits ViewModelBase

Public Sub New()
Source = CustomerDataModel.GetCustomers()
End Sub

Public ReadOnly Property Source As ObservableCollection(Of Customer)
End Class
End Namespace
将GridControl绑定到数据

1. 构建解决方案,使ViewModel类在窗口的Quick Actions中可见。

2. 打开窗口的Quick Actions并指定窗口的数据上下文:

MyEclipse中文使用教程图集

您也可以在代码中定义窗口的数据上下文:

XAML

<Window
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:BindToLocalCollection"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
x:Class="BindToLocalCollection.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Grid>
<dxg:GridControl AutoGenerateColumns="AddNew"
EnableSmartColumnsGeneration="True">
<dxg:GridControl.View>
<dxg:TableView/>
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</Window>

3. 打开GridControl的Quick Actions并指定ItemsSource:

MyEclipse中文使用教程图集

要在代码中将GridControl绑定到数据,请指定DataControlBase.ItemsSource属性:

XAML

<Window
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:BindToLocalCollection"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
x:Class="BindToLocalCollection.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Grid>
<dxg:GridControl AutoGenerateColumns="AddNew"
EnableSmartColumnsGeneration="True"
ItemsSource="{Binding Source}">
<dxg:GridControl.View>
<dxg:TableView/>
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</Window>

4. 运行项目,GridControl为绑定数据源中的所有字段生成列。

MyEclipse中文使用教程图集

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


更多DevExpress线上公开课、中文教程资讯请上中文网获取

关于慧都科技

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

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


标签:

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

文章转载自:慧都网

为你推荐

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


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP