1. 程式人生 > 其它 >介面控制元件DevExpress WinForms MVVM入門指南——資料繫結

介面控制元件DevExpress WinForms MVVM入門指南——資料繫結

獲取工具下載 - DevExpress v21.2

設定網格資料來源

示例應用程式啟動並執行後,是時候用資料填充其檢視了。

重要提示:您可以自由使用任何想要的樣本資料,在此應用程式中,使用了 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 元件,並一次實現兩個有用的操作。

  • 實體被視為實體物件,通常不會在其任何欄位更改時傳送更改通知。 例如,如果您更改現有帳戶的名稱,相關的‘Account’ 實體仍將被視為未更改,使用 SetObjectDataSourceBinding 方法進行繫結將導致監視這些更改並更新繫結。
  • 當目標 UI 元素更改其繫結屬性值時(例如,編輯器的 BaseEdit.EditValue屬性在執行時被修改),繫結將被更新。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 | 下載試用

DevExpress WinForm擁有180+元件和UI庫,能為Windows Forms平臺建立具有影響力的業務解決方案。DevExpress WinForms能完美構建流暢、美觀且易於使用的應用程式,無論是Office風格的介面,還是分析處理大批量的業務資料,它都能輕鬆勝任!


DevExpress技術交流群6:600715373      歡迎一起進群討論

更多DevExpress線上公開課、中文教程資訊請上中文網獲取