彩票走势图

logo DevExpress WinForm中文手册

高级绑定功能


立即下载DevExpress WinForms

转换器

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

默认转换器

DevExpress MVVM框架自动管理简单的类型转换,例如在通过默认转换器绑定的演示中,字符串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事件。您可以处理此事件来调整转换逻辑,绑定自定义转换处理演示演示了一个texttedit编辑器,它的EditValue属性绑定到整数ViewModel Value属性。如果用户让textit为空,则编辑器的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方法参数。第一个转换器将可绑定的属性值转换为可接受的类型,第二个转换器则相反。

通过自定义转换器进行绑定演示演示了一个带有ModelState属性的ViewModel,该属性接受自定义状态枚举值,此属性绑定到CheckBox.CheckState属性,其类型为System.Windows.Forms.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})")

可以添加来应用其他数字、 date-time和时间跨度格式,MVVM最佳实践演示演示了如何将整数值显示为货币。

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方法,这个方法接受以下参数:

  1. 控件名称。
  2. 应该被绑定的控件属性。
  3. 一个字符串数组,其中包含可绑定的ViewModel属性的名称,这些属性的值应该组合在一起。
  4. 格式字符串(对于不可编辑控件)或一对转换器(如果允许用户编辑绑定控件)。

DevExpress演示中心提供了两个模块,它们将FirstName和LastName属性的值组合到一个textit编辑器中。使用格式字符串的模块将属性绑定到禁用的(不可编辑的)编辑器,在使用转换器的模块中,您可以更改TextEdit值并将更新后的字符串传递回ViewModel属性。

  • 格式化字符串演示

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}")
  • 转换器演示

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))
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP