翻譯|使用教程|編輯:龔雪|2022-04-12 09:46:19.283|閱讀 279 次
概述:本系列內(nèi)容將開(kāi)始根據(jù)DevExpress WinForms MVVM創(chuàng)建示例應(yīng)用程序,本文繼續(xù)講解如何綁定數(shù)據(jù)。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
示例應(yīng)用程序啟動(dòng)并運(yùn)行后,是時(shí)候用數(shù)據(jù)填充其視圖了。
重要提示:您可以自由使用任何想要的樣本數(shù)據(jù),在此應(yīng)用程序中,使用了 Expenses.sqlite3 示例數(shù)據(jù)庫(kù)。該文件包含在DevExpress Demo Center的數(shù)據(jù)中,您可以從本地存儲(chǔ)中復(fù)制它(默認(rèn)位置是 C:\Users\Public\Documents\DevExpress Demos 21.2\Components\Data\Expenses.sqlite3)。將此文件包含到您的項(xiàng)目中,并確保應(yīng)用程序配置使用所需的 SQLiteConnection 來(lái)訪問(wèn)它。
所有詳細(xì)視圖都包含以相同方式綁定的 GridControl,由 Scaffolding Wizard 生成的“EntitiesViewModel”文件包含一個(gè)可用作數(shù)據(jù)源的實(shí)體集合。以下代碼說(shuō)明了如何使用 SetBinding 方法將“Accounts”視圖的 GridControl 綁定到其數(shù)據(jù)源。
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)
對(duì)于其余的詳細(xì)視圖,此代碼保持不變。 您只需要修改 ViewModel 的名稱,傳遞給 OfType() 方法。
當(dāng)您的視圖綁定到所需數(shù)據(jù)時(shí),可能希望將網(wǎng)格與 SelectedEntity 對(duì)象同步,該對(duì)象保留當(dāng)前選定的實(shí)體。 為此,請(qǐng)使用將創(chuàng)建此綁定并在每次發(fā)生 ColumnView.FocusedRowObjectChanged 事件時(shí)刷新它的事件到命令操作,下面的代碼說(shuō)明了帳戶視圖的示例。
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))
編輯表單視圖包含數(shù)據(jù)布局控件替代網(wǎng)格控件,綁定本身的概念保持不變。 不同之處在于詳細(xì)視圖顯示整個(gè)實(shí)體集合,因此使用自動(dòng)生成的“EntitiesViewModel”類的屬性和方法。編輯表單視圖旨在顯示單個(gè)實(shí)體,因此在這里您應(yīng)該使用“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 組件,并一次實(shí)現(xiàn)兩個(gè)有用的操作。
作為這些機(jī)制的副作用,用于設(shè)計(jì)編輯表單的 DevExpress 編輯器將開(kāi)始驗(yàn)證最終用戶輸入的值(見(jiàn)下圖)。 此驗(yàn)證基于數(shù)據(jù)注釋屬性,在第一個(gè)教程中在您的數(shù)據(jù)模型中聲明。
Transactions 集合的編輯表單視圖具有更復(fù)雜的布局,帶有下拉編輯器,允許最終用戶從其他兩個(gè)集合(帳戶和類別)中選擇實(shí)體。 因此,您將需要下拉編輯器來(lái)顯示這些集合中的項(xiàng)目。 為此,請(qǐng)將您的 accountBindingSource 和 categoryBindingSource 對(duì)象綁定到所需的集合。
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庫(kù),能為Windows Forms平臺(tái)創(chuàng)建具有影響力的業(yè)務(wù)解決方案。DevExpress WinForms能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無(wú)論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
更多產(chǎn)品正版授權(quán)詳情及優(yōu)惠,歡迎咨詢
DevExpress技術(shù)交流群6:600715373 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)