DevExpress WinForm控件入门指南:WinForms MVVM – 高级绑定功能

转换器

转换器允许您动态转换可绑定的属性值。

默认转换器

DevExpress MVVM 框架自动管理简单的类型转换。 例如,在 Binding via Default Converters 演示中,字符串 TextEdit.Text 属性绑定到整数 ViewModel Progress 属性。 在这里,框架将属性值从 Int32 转换为 String 并返回。

C#

//View codevar fluent = mvvmContext.OfType<ViewModel>();fluent.SetBinding(editor, e => e.Text, x => x.Progress);//ViewModel codepublic class ViewModel {public virtual int Progress { get; set; }}

VB.NET

'View codeDim fluent = mvvmContext.OfType(Of ViewModel)()fluent.SetBinding(editor, Function(e) e.Text, Function(x) x.Progress)'ViewModel codePublic Class ViewModelPublic Overridable Property Progress() As IntegerEnd Class

C#

//View codevar fluent = mvvmContext.OfType<ViewModel>();mvvmContext.BindingConvert += (s, e) => {string strValue = e.Value as string;if(strValue != null) {int intValue;if(int.TryParse(strValue, out intValue))e.Value = intValue;elsee.Value = null;}if(e.Value == null)e.Value = 0;};fluent.SetBinding(editor, e => e.EditValue, x => x.Value);

VB.NET

'View codeDim fluent = mvvmContext.OfType(Of ViewModel)()AddHandler mvvmContext.BindingConvert, Sub(s, e)Dim strValue As String = TryCast(e.Value, String)If strValue IsNot Nothing ThenDim intValue As Integer = NothingIf Integer.TryParse(strValue, intValue) Thene.Value = intValueElsee.Value = NothingEnd IfEnd IfIf e.Value Is Nothing Thene.Value = 0End IfEnd Subfluent.SetBinding(editor, Function(e) e.EditValue, Function(x) x.Value)

自定义转换器

当您使用无法自动转换的复杂属性类型时,您需要传递两个转换器作为最后的 SetBinding 方法参数。 第一个转换器将可绑定属性值转换为可接受的类型,而第二个转换器则相反。

Binding via Custom Converters demo说明了一个带有 ModelState 属性的 ViewModel,该属性接受自定义 State 枚举值,此属性绑定到类型为 System.Windows.Forms.CheckState 的 CheckBox.CheckState 属性,SetBinding 方法中的 Lambda 表达式是转换属性值的转换器。

C#

//View codevar fluent = mvvmContext.OfType<ViewModel>();fluent.SetBinding(check, e => e.CheckState, x => x.ModelState,modelState => {// Convert the ViewModel.State to CheckStateswitch(modelState) {case ViewModel.State.Active:return CheckState.Checked;case ViewModel.State.Inactive:return CheckState.Unchecked;default:return CheckState.Indeterminate;}},checkState => {// Convert back from CheckState to the ViewModel.Stateswitch(checkState) {case CheckState.Checked:return ViewModel.State.Active;case CheckState.Unchecked:return ViewModel.State.Inactive;default:return ViewModel.State.Suspended;}});//ViewModel codepublic class ViewModel {public virtual State ModelState {get;set;}public enum State {Suspended = 0,Inactive = 1,Active = 2}}

VB.NET

Dim fluent = mvvmContext.OfType(Of ViewModel)()fluent.SetBinding(check, Function(e) e.CheckState, Function(x) x.ModelState, Function(modelState)Select Case modelStateCase ViewModel.State.Active[Return] CheckState.CheckedCase ViewModel.State.Inactive[Return] CheckState.UncheckedCase Else[Return] CheckState.IndeterminateEnd SelectEnd Function,Function(checkState)Select Case checkStateCase CheckState.Checked[Return] ViewModel.State.ActiveCase CheckState.Unchecked[Return] ViewModel.State.InactiveCase Else[Return] ViewModel.State.SuspendedEnd SelectEnd Function)'ViewModel codePublic Class ViewModelPublic Overridable Property ModelState() As StatePublic Enum StateSuspended = 0Inactive = 1Active = 2End EnumEnd Class
格式绑定值

要格式化绑定属性值,请将字符串格式表达式传递给 SetBinding 方法,{0} 字符序列是属性值的占位符。

C#

var fluent = mvvmContext.OfType<ViewModel>();fluent.SetBinding(labelControl, l => l.Text, x => x.Value, "Bound property value is ({0})");

VB.NET

Dim fluent = mvvmContext.OfType(Of ViewModel)()fluent.SetBinding(labelControl, Function(l) l.Text, Function(x) x.Value, "Bound property value is ({0})")

您可以添加Format Specifiers来应用其他数字、日期时间和时间跨度格式,MVVM Best Practices demo说明了如何将整数值显示为货币。

C#

var fluent = mvvmContext.OfType<ViewModel>();fluent.SetBinding(label, l => l.Text, x => x.Price, "Price: {0:C2}");

VB.NET

Dim fluent = mvvmContext.OfType(Of ViewModel)()fluent.SetBinding(label, Function(l) l.Text, Function(x) x.Price, "Price: {0:C2}")

将多个属性绑定到同一个控件

要在同一控件中组合多个属性的值,请使用 MvvmContext.SetMultiBinding 方法。 此方法接受以下参数:

  • 控件名称;
  • 应该绑定的控件属性;
  • 一个字符串数组,填充了可绑定的 ViewModel 属性的名称,这些属性的值应该组合在一起;

Format string demo

C#

var fluent = mvvmContext.OfType<ViewModel>();mvvmContext.SetMultiBinding(editForFullName,e => e.Text,new string[] { "FirstName", "LastName" },"{1}, {0}");

VB.NET

Dim fluent = mvvmContext.OfType(Of ViewModel)()mvvmContext.SetMultiBinding(editForFullName, Function(e) e.Text, New String() { "FirstName", "LastName" }, "{1}, {0}")

Converters demo

C#

var fluent = mvvmContext.OfType<ViewModel>();mvvmContext.SetMultiBinding(editForFullName,e => e.EditValue,new string[] { "FirstName", "LastName" },values => string.Join(",", values),value => ((string)value).Split(','));

VB.NET

Dim fluent = mvvmContext.OfType(Of ViewModel)()mvvmContext.SetMultiBinding(editForFullName,Function(e) e.EditValue,New String() { "FirstName", "LastName" },Function(values) String.Join(",", values),Function(value) CStr(value).Split(","c))

DevExpress WinForm | 下载试用

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

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


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

DevExpress v21.2新版本正式发布
标签:

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

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

相关推荐

发表回复

登录后才能评论