翻譯|行業(yè)資訊|編輯:龔雪|2021-11-23 10:18:31.557|閱讀 331 次
概述:DevExpress WinForm創(chuàng)建的應用程序可利用MVVM設計模式,本文主要介紹高級綁定功能,歡迎下載最新版體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
獲取工具下載 - DevExpress WinForm v21.2
轉換器允許您動態(tài)轉換可綁定的屬性值。
默認轉換器
DevExpress MVVM 框架自動管理簡單的類型轉換。 例如,在 Binding via Default Converters 演示中,字符串 TextEdit.Text 屬性綁定到整數(shù) 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 組件會觸發(fā) BindingConvert 事件,您可以處理此事件以調整轉換邏輯。Binding with Custom Conversion Handling demo演示說明了一個 TextEdit 編輯器,其 EditValue 屬性綁定到整數(shù) 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 方法參數(shù)。 第一個轉換器將可綁定屬性值轉換為可接受的類型,而第二個轉換器則相反。
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})")
您可以添加來應用其他數(shù)字、日期時間和時間跨度格式,MVVM Best Practices demo說明了如何將整數(shù)值顯示為貨幣。
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 方法。 此方法接受以下參數(shù):
DevExpress 演示中心提供了兩個模塊,它們將 FirstName 和 LastName 屬性的值組合到一個 TextEdit 編輯器中。 使用格式字符串的模塊將屬性綁定到禁用(不可編輯)的編輯器,在使用轉換器的模塊中,您可以更改 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))
DevExpress WinForm擁有180+組件和UI庫,能為Windows Forms平臺創(chuàng)建具有影響力的業(yè)務解決方案。DevExpress WinForms能完美構建流暢、美觀且易于使用的應用程序,無論是Office風格的界面,還是分析處理大批量的業(yè)務數(shù)據,它都能輕松勝任!
更多產品正版授權詳情及優(yōu)惠,歡迎咨詢
DevExpress技術交流群5:742234706 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網