当前位置: 首页 > news >正文

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

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

MyEclipse中文使用教程图集

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

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

新增一个数据模型

用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.ObjectModelNamespace 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 ClassPublic 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.ObjectModelNamespace BindToLocalCollection
Public Class ViewModel
Inherits ViewModelBasePublic Sub New()
Source = CustomerDataModel.GetCustomers()
End SubPublic 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中文使用教程图集


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

http://www.dtcms.com/a/353641.html

相关文章:

  • 软件定义汽车(SDV)调试——如何做到 适配软件定义汽车(SDV)?(下)
  • vue新能源汽车销售平台的设计与实现(代码+数据库+LW)
  • 【Vue2✨】 Vue2 入门之旅(二):模板语法
  • Python异步编程:从理论到实战的完整指南
  • Qt---项目架构解读
  • BiLSTM-Attention分类预测+SHAP分析+特征依赖图!深度学习可解释分析,Matlab代码实现
  • 【GaussDB】深度解析:创建存储过程卡死且无法Kill会话的疑难排查
  • codeforces(1045)(div2)D. Sliding Tree
  • 装饰器模式(C++python)
  • 第十四章 Leaflet-Ant-Path 实现西气东输管线动态流向可视化
  • 源代码接入 1688 接口的详细指南
  • 【生产事故处理--kafka日志策略保留】
  • antv x6实现封装拖拽流程图配置(适用于工单流程、审批流程应用场景)
  • 使用Stone 3D快速制作第一人称视角在线小游戏
  • STM32八大模式
  • Yapi接口文档导出测试用例至Excel中
  • ProfiNet 转 Ethernet/IP西门子 S7-400 及罗克韦尔 PLC 于原油蒸馏的集成应用
  • 插入排序讲解
  • D‘RespNeT无人机图像分割数据集与YOLOv8-DRN模型,实时识别入口与障碍,助力灾后救援
  • WebConfig的登录与放行
  • 【C语言16天强化训练】从基础入门到进阶:Day 12
  • 归档和压缩
  • 摄像头镜头模组的设计要点
  • ES03-常用API
  • 安装了TortoiseSVN但是在idea的subversion里面找不到svn.exe
  • Dify 从入门到精通(第 59/100 篇):Dify 的自动化测试(进阶篇)
  • Python爬虫实战:构建音乐作品电商平台数据采集与分析系统
  • Highcharts Stock :打造专业级金融图表的利器
  • Apache DolphinScheduler:数据治理中数据质检利器
  • 机器学习 TF-IDF方法