翻譯|行業(yè)資訊|編輯:龔雪|2021-11-23 10:18:31.557|閱讀 331 次
概述:DevExpress WinForm創(chuàng)建的應(yīng)用程序可利用MVVM設(shè)計(jì)模式,本文主要介紹高級(jí)綁定功能,歡迎下載最新版體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
獲取工具下載 - DevExpress WinForm v21.2
轉(zhuǎn)換器允許您動(dòng)態(tài)轉(zhuǎn)換可綁定的屬性值。
默認(rèn)轉(zhuǎn)換器
DevExpress MVVM 框架自動(dòng)管理簡(jiǎn)單的類(lèi)型轉(zhuǎn)換。 例如,在 Binding via Default Converters 演示中,字符串 TextEdit.Text 屬性綁定到整數(shù) ViewModel Progress 屬性。 在這里,框架將屬性值從 Int32 轉(zhuǎn)換為 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
當(dāng)框架轉(zhuǎn)換值時(shí),MvvmContext 組件會(huì)觸發(fā) BindingConvert 事件,您可以處理此事件以調(diào)整轉(zhuǎn)換邏輯。Binding with Custom Conversion Handling demo演示說(shuō)明了一個(gè) TextEdit 編輯器,其 EditValue 屬性綁定到整數(shù) ViewModel Value 屬性。如果用戶(hù)將 TextEdit 留空,則編輯器的 EditValue 為 null,因?yàn)樽詣?dòng)轉(zhuǎn)換無(wú)法將 null 轉(zhuǎn)換為 Int32。 在這種情況下,使用 BindingConvert 事件處理程序?qū)?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)
自定義轉(zhuǎn)換器
當(dāng)您使用無(wú)法自動(dòng)轉(zhuǎn)換的復(fù)雜屬性類(lèi)型時(shí),您需要傳遞兩個(gè)轉(zhuǎn)換器作為最后的 SetBinding 方法參數(shù)。 第一個(gè)轉(zhuǎn)換器將可綁定屬性值轉(zhuǎn)換為可接受的類(lèi)型,而第二個(gè)轉(zhuǎn)換器則相反。
Binding via Custom Converters demo說(shuō)明了一個(gè)帶有 ModelState 屬性的 ViewModel,該屬性接受自定義 State 枚舉值,此屬性綁定到類(lèi)型為 System.Windows.Forms.CheckState 的 CheckBox.CheckState 屬性,SetBinding 方法中的 Lambda 表達(dá)式是轉(zhuǎn)換屬性值的轉(zhuǎn)換器。
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
如果不允許用戶(hù)編輯 View 元素的屬性值,則可以跳過(guò)反向轉(zhuǎn)換。
要格式化綁定屬性值,請(qǐng)將字符串格式表達(dá)式傳遞給 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})")
您可以添加來(lái)應(yīng)用其他數(shù)字、日期時(shí)間和時(shí)間跨度格式,MVVM Best Practices demo說(shuō)明了如何將整數(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}")
將多個(gè)屬性綁定到同一個(gè)控件
要在同一控件中組合多個(gè)屬性的值,請(qǐng)使用 MvvmContext.SetMultiBinding 方法。 此方法接受以下參數(shù):
DevExpress 演示中心提供了兩個(gè)模塊,它們將 FirstName 和 LastName 屬性的值組合到一個(gè) TextEdit 編輯器中。 使用格式字符串的模塊將屬性綁定到禁用(不可編輯)的編輯器,在使用轉(zhuǎn)換器的模塊中,您可以更改 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庫(kù),能為Windows Forms平臺(tái)創(chuàng)建具有影響力的業(yè)務(wù)解決方案。DevExpress WinForms能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無(wú)論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
更多產(chǎn)品正版授權(quán)詳情及優(yōu)惠,歡迎咨詢(xún)
DevExpress技術(shù)交流群5:742234706 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)