DevExpress WinForm控件入门指南:WinForms MVVM – 数据和属性绑定(Part 2)

根据您绑定的属性,存在以下三种可能的情况:

  • 属性依赖 – 来自同一个ViewModel的两个属性被绑定。

绑定嵌套和非POCO视图模型的属性

如果您需要绑定嵌套的ViewModel属性,请使用DevExpress.Mvvm.POCO.ViewModelSource.Create方法创建此嵌套ViewModel的实例,您可以通过父ViewModel访问该实例,视图绑定语法使用相同的SetBinding方法。

C#

//Nested ViewModelpublic class NestedViewModel {public virtual string Text { get; set; }}//Parent ViewModelpublic class ViewModelWithChild {public ViewModelWithChild() {Child = ViewModelSource.Create<NestedViewModel>();}public NestedViewModel Child {get;private set;}}//View codevar fluent = mvvmContext.OfType<ViewModelWithChild>();fluent.SetBinding(editor, ed => ed.EditValue, x => x.Child.Text);

VB.NET

'Nested ViewModelPublic Class NestedViewModelPublic Overridable Property Text() As StringEnd Class'Parent ViewModelPublic Class ViewModelWithChildPublic Sub New()Child = ViewModelSource.Create(Of NestedViewModel)()End SubPrivate privateChild As NestedViewModelPublic Property Child() As NestedViewModelGetReturn privateChildEnd GetPrivate Set(ByVal value As NestedViewModel)privateChild = valueEnd SetEnd PropertyEnd Class'View codeDim 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 codepublic 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 codemvvmContext.SetViewModel(typeof(ObjectWithTextAndTitle), viewModelInstance);var fluent = mvvmContext.OfType<ObjectWithTextAndTitle>();fluent.SetBinding(editor, ed => ed.EditValue, x => x.Text);

VB.NET

'ViewModel codePublic Class ObjectWithTextAndTitlePrivate textCore As StringPublic Property Text() As StringGetReturn textCoreEnd GetSet(ByVal value As String)If textCore = value ThenReturnEnd IftextCore = valueOnTextChanged()End SetEnd PropertyProtected Overridable Sub OnTextChanged()RaiseTextChanged()End SubProtected Sub RaiseTextChanged()Dim handler = TextChangedEventIf handler IsNot Nothing Thenhandler(Me, EventArgs.Empty)End IfEnd SubPublic Event TextChanged As EventHandlerEnd Class'View codemvvmContext.SetViewModel(GetType(ObjectWithTextAndTitle), viewModelInstance)Dim fluent = mvvmContext.OfType(Of ObjectWithTextAndTitle)()fluent.SetBinding(editor, Function(ed) ed.EditValue, Function(x) x.Text)
数据绑定

C#

editor.DataBindings.Add(...);

VB.NET

editor.DataBindings.Add(...)

C#

//ViewmvvmContext.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 calledBindingSource entityBindingSource = new BindingSource();entityBindingSource.DataSource = typeof(Entity);fluentApi.SetObjectDataSourceBinding(entityBindingSource, x => x.Entity, x => x.Update());// Data BindingsidEditor.DataBindings.Add(new Binding("EditValue", entityBindingSource, "ID"));textEditor.DataBindings.Add(new Binding("EditValue", entityBindingSource, "Text", true, DataSourceUpdateMode.OnPropertyChanged));//ViewModelpublic class ViewModel {//...public virtual Entity Entity {get;set;}//...}//Modelpublic 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

'ViewmvvmContext.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 calledDim entityBindingSource As New BindingSource()entityBindingSource.DataSource = GetType(Entity)fluentApi.SetObjectDataSourceBinding(entityBindingSource, Function(x) x.Entity, Function(x) x.Update())' Data BindingsidEditor.DataBindings.Add(New Binding("EditValue", entityBindingSource, "ID"))textEditor.DataBindings.Add(New Binding("EditValue", entityBindingSource, "Text", True, DataSourceUpdateMode.OnPropertyChanged))'ViewModelPublic Class ViewModel'...Public Overridable Property Entity() As Entity'...End Class'ModelPublic Class EntityPublic Sub New(ByVal id As Integer)Me.ID = idMe.Text = "Entity " & id.ToString()End SubPrivate privateID As IntegerPublic Property ID() As IntegerGetReturn privateIDEnd GetPrivate Set(ByVal value As Integer)privateID = valueEnd SetEnd PropertyPublic Property Text() As StringEnd Class

DevExpress WinForm | 下载试用

DevExpress WinForm拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!

更多产品正版授权详情及优惠,欢迎咨询在线客服>>


DevExpress技术交流群5:742234706      欢迎一起进群讨论

DevExpress企业化定制服务
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年10月5日
下一篇 2021年10月5日

相关推荐

发表回复

登录后才能评论