翻譯|使用教程|編輯:龔雪|2021-11-08 10:07:11.067|閱讀 289 次
概述:DevExpress WinForm創(chuàng)建的應(yīng)用程序可利用MVVM設(shè)計(jì)模式,本文主要為大家介紹這其中的第二種綁定嵌套和非POCO視圖模型的屬性,歡迎下載最新版體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
根據(jù)您綁定的屬性,存在以下三種可能的情況:
獲取工具下載 - DevExpress WinForm v21.2
如果您需要綁定嵌套的ViewModel屬性,請(qǐng)使用DevExpress.Mvvm.POCO.ViewModelSource.Create方法創(chuàng)建此嵌套ViewModel的實(shí)例,您可以通過父ViewModel訪問該實(shí)例,視圖綁定語法使用相同的SetBinding方法。
C#
//Nested ViewModel public class NestedViewModel { public virtual string Text { get; set; } } //Parent ViewModel public class ViewModelWithChild { public ViewModelWithChild() { Child = ViewModelSource.Create<NestedViewModel>(); } public NestedViewModel Child { get; private set; } } //View code var fluent = mvvmContext.OfType<ViewModelWithChild>(); fluent.SetBinding(editor, ed => ed.EditValue, x => x.Child.Text);
VB.NET
'Nested ViewModel Public Class NestedViewModel Public Overridable Property Text() As String End Class 'Parent ViewModel Public Class ViewModelWithChild Public Sub New() Child = ViewModelSource.Create(Of NestedViewModel)() End Sub Private privateChild As NestedViewModel Public Property Child() As NestedViewModel Get Return privateChild End Get Private Set(ByVal value As NestedViewModel) privateChild = value End Set End Property End Class 'View code Dim fluent = mvvmContext.OfType(Of ViewModelWithChild)() fluent.SetBinding(editor, Function(ed) ed.EditValue, Function(x) x.Child.Text)
如果您不使用POCO模型,則框架不會(huì)自動(dòng)發(fā)送更新通知。 要在這種情況下發(fā)送通知,請(qǐng)實(shí)現(xiàn)INotifyPropertyChanged接口或創(chuàng)建 - PropertyName-Changed 事件。請(qǐng)注意,您不能使用mvvmContext.ViewModelType屬性,您應(yīng)該調(diào)用mvvmContext.SetViewModel方法將ViewModel實(shí)例傳遞給組件。
C#
//ViewModel code public class ObjectWithTextAndTitle { string textCore; public string Text { get { return textCore; } set { if(textCore == value) return; textCore = value; OnTextChanged(); } } protected virtual void OnTextChanged() { RaiseTextChanged(); } protected void RaiseTextChanged() { var handler = TextChanged; if(handler != null) handler(this, EventArgs.Empty); } public event EventHandler TextChanged; } //View code mvvmContext.SetViewModel(typeof(ObjectWithTextAndTitle), viewModelInstance); var fluent = mvvmContext.OfType<ObjectWithTextAndTitle>(); fluent.SetBinding(editor, ed => ed.EditValue, x => x.Text);
VB.NET
'ViewModel code Public Class ObjectWithTextAndTitle Private textCore As String Public Property Text() As String Get Return textCore End Get Set(ByVal value As String) If textCore = value Then Return End If textCore = value OnTextChanged() End Set End Property Protected Overridable Sub OnTextChanged() RaiseTextChanged() End Sub Protected Sub RaiseTextChanged() Dim handler = TextChangedEvent If handler IsNot Nothing Then handler(Me, EventArgs.Empty) End If End Sub Public Event TextChanged As EventHandler End Class 'View code mvvmContext.SetViewModel(GetType(ObjectWithTextAndTitle), viewModelInstance) Dim fluent = mvvmContext.OfType(Of ObjectWithTextAndTitle)() fluent.SetBinding(editor, Function(ed) ed.EditValue, Function(x) x.Text)
要將編輯器綁定到模型屬性,請(qǐng)將BindingSource添加到視圖并使用標(biāo)準(zhǔn)DataBindings API。可選的 updateMode 參數(shù)允許您指定屬性是否在編輯器值更改時(shí)更新其值,以及(如果是)是應(yīng)該立即發(fā)生還是在驗(yàn)證編輯器時(shí)發(fā)生。
C#
editor.DataBindings.Add(...);
VB.NET
editor.DataBindings.Add(...)
實(shí)體屬性綁定演示定義了一個(gè)自定義實(shí)體類,此類的實(shí)例用作數(shù)據(jù)記錄并具有 ID 和文本字段。 兩個(gè)數(shù)據(jù)字段都綁定到編輯器,BindingSource 組件存儲(chǔ)活動(dòng)的實(shí)體對(duì)象。
C#
//View mvvmContext.ViewModelType = typeof(ViewModel); var fluentApi = mvvmContext.OfType<ViewModel>(); // Create a BindingSource and populate it with a data object. //When a user modifies this object, the "Update" method is called BindingSource entityBindingSource = new BindingSource(); entityBindingSource.DataSource = typeof(Entity); fluentApi.SetObjectDataSourceBinding(entityBindingSource, x => x.Entity, x => x.Update()); // Data Bindings idEditor.DataBindings.Add( new Binding("EditValue", entityBindingSource, "ID")); textEditor.DataBindings.Add( new Binding("EditValue", entityBindingSource, "Text", true, DataSourceUpdateMode.OnPropertyChanged)); //ViewModel public class ViewModel { //... public virtual Entity Entity { get; set; } //... } //Model public class Entity { public Entity(int id) { this.ID = id; this.Text = "Entity " + id.ToString(); } public int ID { get; private set; } public string Text { get; set; } }
VB.NET
'View mvvmContext.ViewModelType = GetType(ViewModel) Dim fluentApi = mvvmContext.OfType(Of ViewModel)() ' Create a BindingSource and populate it with a data object. 'When a user modifies this object, the "Update" method is called Dim entityBindingSource As New BindingSource() entityBindingSource.DataSource = GetType(Entity) fluentApi.SetObjectDataSourceBinding(entityBindingSource, Function(x) x.Entity, Function(x) x.Update()) ' Data Bindings idEditor.DataBindings.Add(New Binding("EditValue", entityBindingSource, "ID")) textEditor.DataBindings.Add(New Binding("EditValue", entityBindingSource, "Text", True, DataSourceUpdateMode.OnPropertyChanged)) 'ViewModel Public Class ViewModel '... Public Overridable Property Entity() As Entity '... End Class 'Model Public Class Entity Public Sub New(ByVal id As Integer) Me.ID = id Me.Text = "Entity " & id.ToString() End Sub Private privateID As Integer Public Property ID() As Integer Get Return privateID End Get Private Set(ByVal value As Integer) privateID = value End Set End Property Public Property Text() As String End Class
DevExpress WinForm擁有180+組件和UI庫,能為Windows Forms平臺(tái)創(chuàng)建具有影響力的業(yè)務(wù)解決方案。DevExpress WinForms能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
更多產(chǎn)品正版授權(quán)詳情及優(yōu)惠,歡迎咨詢
DevExpress技術(shù)交流群5:742234706 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)