DevExpress控件使用交流,DevExpress中国社区Dev联系电话 联系电话:023-68661681

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

来源:   发布时间:2021-11-08   浏览:1066次

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

  • 常规绑定 - ViewModel属性绑定到任何不可编辑的View元素属性。由于该元素不可编辑,因此您无需将更新通知发送回绑定属性(单向绑定)。
  • 数据绑定 - Model属性(数据字段)绑定到编辑器属性。如果用户可以更改编辑器值,则需要更新绑定属性(双向绑定)。
  • 属性依赖 - 来自同一个ViewModel的两个属性被绑定。

获取工具下载 - DevExpress WinForm v21.2

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

如果您需要绑定嵌套的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 | 下载试用

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

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


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

更多DevExpress线上公开课、中文教程资讯请上中文网获取

DevExpress企业化定制服务
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/2732.html

相关产品: DevExpress Universal Subscription,

在线
客服
微信
QQ 电话
023-68661681
返回
顶部