翻譯|使用教程|編輯:龔雪|2022-04-12 09:46:19.283|閱讀 279 次
概述:本系列內容將開始根據DevExpress WinForms MVVM創建示例應用程序,本文繼續講解如何綁定數據。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
示例應用程序啟動并運行后,是時候用數據填充其視圖了。
重要提示:您可以自由使用任何想要的樣本數據,在此應用程序中,使用了 Expenses.sqlite3 示例數據庫。該文件包含在DevExpress Demo Center的數據中,您可以從本地存儲中復制它(默認位置是 C:\Users\Public\Documents\DevExpress Demos 21.2\Components\Data\Expenses.sqlite3)。將此文件包含到您的項目中,并確保應用程序配置使用所需的 SQLiteConnection 來訪問它。
所有詳細視圖都包含以相同方式綁定的 GridControl,由 Scaffolding Wizard 生成的“EntitiesViewModel”文件包含一個可用作數據源的實體集合。以下代碼說明了如何使用 SetBinding 方法將“Accounts”視圖的 GridControl 綁定到其數據源。
C#
var fluent = mvvmContext1.OfType<AccountCollectionViewModel>(); fluent.SetBinding(gridView1, gView => gView.LoadingPanelVisible, x => x.IsLoading); fluent.SetBinding(gridControl1, gControl => gControl.DataSource, x => x.Entities);
VB.NET
Dim fluent = mvvmContext1.OfType(Of AccountCollectionViewModel)() fluent.SetBinding(gridView1, Function(gView) gView.LoadingPanelVisible, Function(x) x.IsLoading) fluent.SetBinding(gridControl1, Function(gControl) gControl.DataSource, Function(x) x.Entities)
對于其余的詳細視圖,此代碼保持不變。 您只需要修改 ViewModel 的名稱,傳遞給 OfType() 方法。
當您的視圖綁定到所需數據時,可能希望將網格與 SelectedEntity 對象同步,該對象保留當前選定的實體。 為此,請使用將創建此綁定并在每次發生 ColumnView.FocusedRowObjectChanged 事件時刷新它的事件到命令操作,下面的代碼說明了帳戶視圖的示例。
C#
fluent.WithEvent<DevExpress.XtraGrid.Views.Base.ColumnView, DevExpress.XtraGrid.Views.Base.FocusedRowObjectChangedEventArgs>(gridView1, "FocusedRowObjectChanged") .SetBinding(x => x.SelectedEntity, args => args.Row as DataModels.Account, (gView, entity) => gView.FocusedRowHandle = gView.FindRow(entity));
VB.NET
fluent.WithEvent(Of DevExpress.XtraGrid.Views.Base.ColumnView, DevExpress.XtraGrid.Views.Base.FocusedRowObjectChangedEventArgs)(gridView1, "FocusedRowObjectChanged").SetBinding(Function(x) x.SelectedEntity, Function(args) TryCast(args.Row, DataModels.Account), Sub(gView, entity) gView.FocusedRowHandle = gView.FindRow(entity))
編輯表單視圖包含數據布局控件替代網格控件,綁定本身的概念保持不變。 不同之處在于詳細視圖顯示整個實體集合,因此使用自動生成的“EntitiesViewModel”類的屬性和方法。編輯表單視圖旨在顯示單個實體,因此在這里您應該使用“SingleObjectViewModel”類的方法和屬性。
C#
//Account Edit Form View var fluent = mvvmContext1.OfType<AccountViewModel>(); fluent.SetObjectDataSourceBinding( accountBindingSource, x => x.Entity, x => x.Update()); //Category Edit Form View var fluent = mvvmContext.OfType<CategoryViewModel>(); fluent.SetObjectDataSourceBinding( bindingSource, x => x.Entity, x => x.Update()); //Transaction Edit Form View var fluent = mvvmContext.OfType<TransactionViewModel>(); fluent.SetObjectDataSourceBinding( bindingSource, x => x.Entity, x => x.Update());
VB.NET
'Account Edit Form View Dim fluent = mvvmContext1.OfType(Of AccountViewModel)() fluent.SetObjectDataSourceBinding(accountBindingSource, Function(x) x.Entity, Function(x) x.Update()) 'Category Edit Form View Dim fluent = mvvmContext.OfType(Of CategoryViewModel)() fluent.SetObjectDataSourceBinding(bindingSource, Function(x) x.Entity, Function(x) x.Update()) 'Transaction Edit Form View Dim fluent = mvvmContext.OfType(Of TransactionViewModel)() fluent.SetObjectDataSourceBinding(bindingSource, Function(x) x.Entity, Function(x) x.Update())
在上面的代碼中,使用了 SetObjectDataSourceBinding 方法。 此方法專門用于 Binding Source 組件,并一次實現兩個有用的操作。
作為這些機制的副作用,用于設計編輯表單的 DevExpress 編輯器將開始驗證最終用戶輸入的值(見下圖)。 此驗證基于數據注釋屬性,在第一個教程中在您的數據模型中聲明。
Transactions 集合的編輯表單視圖具有更復雜的布局,帶有下拉編輯器,允許最終用戶從其他兩個集合(帳戶和類別)中選擇實體。 因此,您將需要下拉編輯器來顯示這些集合中的項目。 為此,請將您的 accountBindingSource 和 categoryBindingSource 對象綁定到所需的集合。
C#
fluent.SetBinding(accountBindingSource, abs => abs.DataSource, x => x.LookUpAccounts.Entities); fluent.SetBinding(categoryBindingSource, cbs => cbs.DataSource, x => x.LookUpCategories.Entities);
VB.NET
fluent.SetBinding(accountBindingSource, Function(abs) abs.DataSource, Function(x) x.LookUpAccounts.Entities) fluent.SetBinding(categoryBindingSource, Function(cbs) cbs.DataSource, Function(x) x.LookUpCategories.Entities)
DevExpress WinForm擁有180+組件和UI庫,能為Windows Forms平臺創建具有影響力的業務解決方案。DevExpress WinForms能完美構建流暢、美觀且易于使用的應用程序,無論是Office風格的界面,還是分析處理大批量的業務數據,它都能輕松勝任!
更多產品正版授權詳情及優惠,歡迎咨詢
DevExpress技術交流群6:600715373 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網