• <menu id="w2i4a"></menu>
  • logo DevExpress WinForm中文手冊(cè)

    高級(jí)綁定功能


    立即下載DevExpress WinForms

    轉(zhuǎn)換器

    轉(zhuǎn)換器允許您動(dòng)態(tài)地轉(zhuǎn)換可綁定的屬性值。

    默認(rèn)轉(zhuǎn)換器

    DevExpress MVVM框架自動(dòng)管理簡(jiǎn)單的類(lèi)型轉(zhuǎn)換,例如在通過(guò)默認(rèn)轉(zhuǎn)換器綁定的演示中,字符串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事件。您可以處理此事件來(lái)調(diào)整轉(zhuǎn)換邏輯,綁定自定義轉(zhuǎn)換處理演示演示了一個(gè)texttedit編輯器,它的EditValue屬性綁定到整數(shù)ViewModel Value屬性。如果用戶(hù)讓textit為空,則編輯器的EditValue為null,因?yàn)樽詣?dòng)轉(zhuǎn)換不能將null轉(zhuǎn)換為Int32,在這種情況下,使用BindingConvert事件處理程序?qū)ull更改為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)使用不能自動(dòng)轉(zhuǎn)換的復(fù)雜屬性類(lèi)型時(shí),需要傳遞兩個(gè)轉(zhuǎn)換器作為最后一個(gè)SetBinding方法參數(shù)。第一個(gè)轉(zhuǎn)換器將可綁定的屬性值轉(zhuǎn)換為可接受的類(lèi)型,第二個(gè)轉(zhuǎn)換器則相反。

    通過(guò)自定義轉(zhuǎn)換器進(jìn)行綁定演示演示了一個(gè)帶有ModelState屬性的ViewModel,該屬性接受自定義狀態(tài)枚舉值,此屬性綁定到CheckBox.CheckState屬性,其類(lèi)型為System.Windows.Forms.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})")

    可以添加格式說(shuō)明符來(lái)應(yīng)用其他數(shù)字、 date-time和時(shí)間跨度格式,MVVM最佳實(shí)踐演示演示了如何將整數(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方法,這個(gè)方法接受以下參數(shù):

    1. 控件名稱(chēng)。
    2. 應(yīng)該被綁定的控件屬性。
    3. 一個(gè)字符串?dāng)?shù)組,其中包含可綁定的ViewModel屬性的名稱(chēng),這些屬性的值應(yīng)該組合在一起。
    4. 格式字符串(對(duì)于不可編輯控件)或一對(duì)轉(zhuǎn)換器(如果允許用戶(hù)編輯綁定控件)。

    DevExpress演示中心提供了兩個(gè)模塊,它們將FirstName和LastName屬性的值組合到一個(gè)textit編輯器中。使用格式字符串的模塊將屬性綁定到禁用的(不可編輯的)編輯器,在使用轉(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}")
    • 轉(zhuǎn)換器演示

    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))
    掃碼咨詢(xún)


    添加微信 立即咨詢(xún)

    電話咨詢(xún)

    客服熱線
    023-68661681

    TOP
    三级成人熟女影院,欧美午夜成人精品视频,亚洲国产成人乱色在线观看,色中色成人论坛 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();