翻譯|使用教程|編輯:龔雪|2025-08-26 10:02:27.877|閱讀 47 次
概述:本文主要介紹DevExpress WPF Grid控件如何將數(shù)據(jù)綁定到本地集合,歡迎下載最新版組件體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
DevExpress WPF擁有120+個控件和庫,將幫助您交付滿足甚至超出企業(yè)需求的高性能業(yè)務(wù)應(yīng)用程序。通過DevExpress WPF能創(chuàng)建有著強大互動功能的XAML基礎(chǔ)應(yīng)用程序,這些應(yīng)用程序?qū)?注于當代客戶的需求和構(gòu)建未來新一代支持觸摸的解決方案。 無論是Office辦公軟件的衍伸產(chǎn)品,還是以數(shù)據(jù)為中心的商業(yè)智能產(chǎn)品,都能通過DevExpress WPF控件來實現(xiàn)。
本文主要演示如何將DevExpress WPF GridControl綁定到本地集合,歡迎下載最新版組件體驗!
DevExpress技術(shù)交流群11:749942875 歡迎一起進群討論
用Customer類的集合創(chuàng)建一個數(shù)據(jù)模型:
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
創(chuàng)建一個從CustomerDataModel接收數(shù)據(jù)的視圖模型類:
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. 構(gòu)建解決方案,使ViewModel類在窗口的Quick Actions中可見。
2. 打開窗口的Quick Actions并指定窗口的數(shù)據(jù)上下文:
您也可以在代碼中定義窗口的數(shù)據(jù)上下文:
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綁定到數(shù)據(jù),請指定屬性:
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為綁定數(shù)據(jù)源中的所有字段生成列。
更多產(chǎn)品資訊及授權(quán),歡迎來電咨詢:023-68661681
慧都是?家?業(yè)數(shù)字化解決?案公司,專注于軟件、?油與?業(yè)領(lǐng)域,以深?的業(yè)務(wù)理解和?業(yè)經(jīng)驗,幫助企業(yè)實現(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ā)并強化交互體驗。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)