没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:龚雪|2021-11-08 10:07:11.067|阅读 221 次
概述: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@wqylolg.cn
文章转载自:慧都网本文将为大家介绍DevExpress XAF如何将.NET Aspire集成到Blazor项目中,欢迎下载最新版组件体验!
JxBrowser是 TeamDev 开发的跨平台库,用于在 Java 应用程序中集成 Chromium 浏览器。它支持 HTML5、CSS3、JavaScript 等,具备硬件加速渲染、双向 Java 与 JavaScript 连接、丰富的事件监听等功能,能处理网页保存、打印等操作,助力 Java 应用高效展示和交互网页内容。
FastReport 不仅仅包含报表组件,它还提供更多功能!特别是,它有几个组件可以像其他 Delphi 组件一样在您的应用程序中使用,而且我们一直在向库中添加更多组件。在本文中,我们将介绍其中之一:TfrShellTreeView。
RFID 标签是一种现代化的产品识别方式,正在迅速取代条形码。RFID 标签的独特之处在于它使用无线电信号。这让您可以快速扫描大量物品,节省大量时间。RFID 标签也用于识别公司内部的员工。在本文中,我们将探讨 RFID 标签如何与FastReport VCL中新增的 TfrxDeviceCommand 对象配合使用。
优秀的界面控件开发包,帮助企业构建卓越应用!
DevExpress DXperience Subscription高性价比的企业级.NET用户界面套包,助力企业创建卓越应用!
DevExpress WinForms Subscription为Windows Forms平台创建具有影响力的业务解决方案,高性价比WinForms界面控件套包。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@wqylolg.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢