欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WinForm应用界面美化攻略 - MVVM - 高级绑定功能 WinFormDevExpressMVVMUI 

程序员文章站 2022-06-01 21:14:52
...

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

转换器

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

默认转换器

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

C#

 

//View code
var fluent = mvvmContext.OfType<ViewModel>();
fluent.SetBinding(editor, e => e.Text, x => x.Progress);

//ViewModel code
public class ViewModel {
public virtual int Progress { get; set; }
}

 

VB.NET

 

'View code
Dim fluent = mvvmContext.OfType(Of ViewModel)()
fluent.SetBinding(editor, Function(e) e.Text, Function(x) x.Progress)

'ViewModel code
Public Class ViewModel
Public Overridable Property Progress() As Integer
End Class

 

当框架转换值时,MvvmContext 组件会触发 BindingConvert 事件,您可以处理此事件以调整转换逻辑。Binding with Custom Conversion Handling demo演示说明了一个 TextEdit 编辑器,其 EditValue 属性绑定到整数 ViewModel Value 属性。如果用户将 TextEdit 留空,则编辑器的 EditValue 为 null,因为自动转换无法将 null 转换为 Int32。 在这种情况下,使用 BindingConvert 事件处理程序将 null 更改为 0。

C#

 

//View code
var 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;
else
e.Value = null;
}
if(e.Value == null)
e.Value = 0;
};
fluent.SetBinding(editor, e => e.EditValue, x => x.Value);

 

VB.NET

 

'View code
Dim fluent = mvvmContext.OfType(Of ViewModel)()
AddHandler mvvmContext.BindingConvert, Sub(s, e)
Dim strValue As String = TryCast(e.Value, String)
If strValue IsNot Nothing Then
Dim intValue As Integer = Nothing
If Integer.TryParse(strValue, intValue) Then
e.Value = intValue
Else
e.Value = Nothing
End If
End If
If e.Value Is Nothing Then
e.Value = 0
End If
End Sub
fluent.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 code
var fluent = mvvmContext.OfType<ViewModel>();
fluent.SetBinding(check, e => e.CheckState, x => x.ModelState,
modelState => {
// Convert the ViewModel.State to CheckState
switch(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.State
switch(checkState) {
case CheckState.Checked:
return ViewModel.State.Active;
case CheckState.Unchecked:
return ViewModel.State.Inactive;
default:
return ViewModel.State.Suspended;
}
});

//ViewModel code
public 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 modelState
Case ViewModel.State.Active
[Return] CheckState.Checked
Case ViewModel.State.Inactive
[Return] CheckState.Unchecked
Case Else
[Return] CheckState.Indeterminate
End Select
End Function,
Function(checkState)
Select Case checkState
Case CheckState.Checked
[Return] ViewModel.State.Active
Case CheckState.Unchecked
[Return] ViewModel.State.Inactive
Case Else
[Return] ViewModel.State.Suspended
End Select
End Function)

'ViewModel code
Public Class ViewModel
Public Overridable Property ModelState() As State
Public Enum State
Suspended = 0
Inactive = 1
Active = 2
End Enum
End Class

 

如果不允许用户编辑 View 元素的属性值,则可以跳过反向转换。

格式绑定值

要格式化绑定属性值,请将字符串格式表达式传递给 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 属性的名称,这些属性的值应该组合在一起;
  • 一个格式字符串(对于不可编辑控件)或一对转换器(如果允许用户编辑绑定控件)。

DevExpress 演示中心提供了两个模块,它们将 FirstName 和 LastName 属性的值组合到一个 TextEdit 编辑器中。 使用格式字符串的模块将属性绑定到禁用(不可编辑)的编辑器,在使用转换器的模块中,您可以更改 TextEdit 值并将更新后的字符串传递回 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线上公开课、中文教程资讯请上中文网获取

WinForm应用界面美化攻略 - MVVM - 高级绑定功能
            
    
    
        WinFormDevExpressMVVMUI