翻譯|使用教程|編輯:龔雪|2021-11-08 10:07:11.067|閱讀 289 次
概述:DevExpress WinForm創建的應用程序可利用MVVM設計模式,本文主要為大家介紹這其中的第二種綁定嵌套和非POCO視圖模型的屬性,歡迎下載最新版體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
根據您綁定的屬性,存在以下三種可能的情況:
獲取工具下載 - DevExpress WinForm v21.2
如果您需要綁定嵌套的ViewModel屬性,請使用DevExpress.Mvvm.POCO.ViewModelSource.Create方法創建此嵌套ViewModel的實例,您可以通過父ViewModel訪問該實例,視圖綁定語法使用相同的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模型,則框架不會自動發送更新通知。 要在這種情況下發送通知,請實現INotifyPropertyChanged接口或創建 - PropertyName-Changed 事件。請注意,您不能使用mvvmContext.ViewModelType屬性,您應該調用mvvmContext.SetViewModel方法將ViewModel實例傳遞給組件。
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)
要將編輯器綁定到模型屬性,請將BindingSource添加到視圖并使用標準DataBindings API。可選的 updateMode 參數允許您指定屬性是否在編輯器值更改時更新其值,以及(如果是)是應該立即發生還是在驗證編輯器時發生。
C#
editor.DataBindings.Add(...);
VB.NET
editor.DataBindings.Add(...)
實體屬性綁定演示定義了一個自定義實體類,此類的實例用作數據記錄并具有 ID 和文本字段。 兩個數據字段都綁定到編輯器,BindingSource 組件存儲活動的實體對象。
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平臺創建具有影響力的業務解決方案。DevExpress WinForms能完美構建流暢、美觀且易于使用的應用程序,無論是Office風格的界面,還是分析處理大批量的業務數據,它都能輕松勝任!
更多產品正版授權詳情及優惠,歡迎咨詢
DevExpress技術交流群5:742234706 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網