翻譯|使用教程|編輯:龔雪|2025-08-26 10:02:27.877|閱讀 47 次
概述:本文主要介紹DevExpress WPF Grid控件如何將數據綁定到本地集合,歡迎下載最新版組件體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
DevExpress WPF擁有120+個控件和庫,將幫助您交付滿足甚至超出企業需求的高性能業務應用程序。通過DevExpress WPF能創建有著強大互動功能的XAML基礎應用程序,這些應用程序專注于當代客戶的需求和構建未來新一代支持觸摸的解決方案。 無論是Office辦公軟件的衍伸產品,還是以數據為中心的商業智能產品,都能通過DevExpress WPF控件來實現。
本文主要演示如何將DevExpress WPF GridControl綁定到本地集合,歡迎下載最新版組件體驗!
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
1. 構建解決方案,使ViewModel類在窗口的Quick Actions中可見。
2. 打開窗口的Quick Actions并指定窗口的數據上下文:
您也可以在代碼中定義窗口的數據上下文:
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:
要在代碼中將GridControl綁定到數據,請指定屬性:
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為綁定數據源中的所有字段生成列。
更多產品資訊及授權,歡迎來電咨詢:023-68661681
慧都是?家?業數字化解決?案公司,專注于軟件、?油與?業領域,以深?的業務理解和?業經驗,幫助企業實現智能化轉型與持續競爭優勢。
慧都是DevExpress的中國區的合作伙伴,DevExpress作為用戶界面領域的優秀產品,幫助企業高效構建權限管理、數據可視化(如網格/圖表/儀表盤)、跨平臺系統(WinForms/ASP.NET/.NET MAUI)及行業定制解決方案,加速開發并強化交互體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網