<strike id="ca4is"><em id="ca4is"></em></strike>
  • <sup id="ca4is"></sup>
    • <s id="ca4is"><em id="ca4is"></em></s>
      <option id="ca4is"><cite id="ca4is"></cite></option>
    • 二維碼
      企資網

      掃一掃關注

      當前位置: 首頁 » 企資快訊 » 匯總 » 正文

      DevExpress_WinForm_MVVM

      放大字體  縮小字體 發布日期:2021-11-16 13:46:59    作者:葉梓盛    瀏覽次數:2
      導讀

      根據您綁定得屬性,存在以下三種可能得情況:常規綁定 - ViewModel屬性綁定到任何不可感謝得View元素屬性。由于該元素不可感謝,因此您無需將更新通知發送回綁定屬性(單向綁定)。數據綁定 - Model屬性(數據字段)

      根據您綁定得屬性,存在以下三種可能得情況:

    • 常規綁定 - ViewModel屬性綁定到任何不可感謝得View元素屬性。由于該元素不可感謝,因此您無需將更新通知發送回綁定屬性(單向綁定)。
    • 數據綁定 - Model屬性(數據字段)綁定到感謝器屬性。如果用戶可以更改感謝器值,則需要更新綁定屬性(雙向綁定)。
    • 屬性依賴 - 來自同一個ViewModel得兩個屬性被綁定。

      DevExpress Universal Subscription自家蕞新版免費下載試用,歷史版本下載,在線文檔和幫助文件下載-慧都網

      屬性依賴

      屬性依賴是來自同一個ViewModel得兩個屬性之間得關系,當一個屬性發生變化時,另一個屬性會更新其值。

      在“MVVM 可靠些實踐”演示中,多個模塊演示了以下設置:

    • 兩個TextEdit控件綁定到ViewModel“Operand1”和“Operand2”屬性。
    • 當用戶更改 TextEdit 值時,操作數屬性會刷新其值。
    • 當操作數屬性更改時,它們會更新數字 “Result”屬性(依賴項 #1)。
    • “Result”屬性更新字符串“ResultText”屬性(依賴項#2)。

      對于使用示例 UI 得每個演示模塊,將 View 元素綁定到 ViewModel 屬性得代碼都是相同得。

      C#

      mvvmContext.ViewModelType = typeof(MultViewModel);var fluentAPI = mvvmContext.OfType<MultViewModel>();fluentAPI.SetBinding(editor1, e => e.EditValue, x => x.Operand1);fluentAPI.SetBinding(editor2, e => e.EditValue, x => x.Operand2);fluentAPI.SetBinding(resultLabel, l => l.Text, x => x.ResultText);

      VB.NET

      mvvmContext.ViewModelType = GetType(MultViewModel)Dim fluentAPI = mvvmContext.OfType(Of MultViewModel)()fluentAPI.SetBinding(editor1, Sub(e) e.EditValue, Sub(x) x.Operand1)fluentAPI.SetBinding(editor2, Sub(e) e.EditValue, Sub(x) x.Operand2)fluentAPI.SetBinding(resultLabel, Sub(l) l.Text, Sub(x) x.ResultText)

      然而,屬性依賴在每個模塊中得聲明都不同。

      onPropertyChanged 方法

      在POCO ViewModels中,您可以聲明OnXChanged方法,其中 X 是屬性名稱。 當相關屬性得值發生變化時,框架會調用這些方法。

      C#

      public class MultViewModel {public virtual int Operand1 { get; set; }public virtual int Operand2 { get; set; }public virtual int Result { get; set; }public virtual string ResultText { get; set; }protected void OnOperand1Changed() {UpdateResult();}protected void OnOperand2Changed() {UpdateResult();}protected void onResultChanged() {UpdateResultText();}void UpdateResult() {Result = Operand1 * Operand2;}void UpdateResultText() {ResultText = string.Format("The result is: {0:n0}", Result);}}

      VB.NET

      Public Class MultViewModelPublic Overridable Property Operand1() As IntegerPublic Overridable Property Operand2() As IntegerPublic Overridable Property Result() As IntegerPublic Overridable Property ResultText() As StringProtected Sub OnOperand1Changed()UpdateResult()End SubProtected Sub OnOperand2Changed()UpdateResult()End SubProtected Sub onResultChanged()UpdateResultText()End SubPrivate Sub UpdateResult()Result = Operand1 * Operand2End SubPrivate Sub UpdateResultText()ResultText = String.Format("The result is: {0:n0}", Result)End SubEnd Class

      自定義更新方法

      如果您得更新方法未被稱為“On...Changed”,請使用 DevExpress.Mvvm.DataAnnotations.BindableProperty 屬性告訴框架它應該在屬性值更改時調用此方法。 在下面得代碼示例中,DevExpress.Mvvm.POCO.RaisePropertyChanged 是一個 DevExpress 擴展方法,它將更新通知發送到依賴屬性。

      C#

      public class SumViewModel {[BindableProperty(onPropertyChangedMethodName = "NotifyResultAndResultTextChanged")]public virtual int Operand1 { get; set; }[BindableProperty(onPropertyChangedMethodName = "NotifyResultAndResultTextChanged")]public virtual int Operand2 { get; set; }public int Result {get { return Operand1 + Operand2; }}public string ResultText {get { return string.Format("The result is: {0:n0}", Result); }}protected void NotifyResultAndResultTextChanged() {this.RaisePropertyChanged(x => x.Result);this.RaisePropertyChanged(x => x.ResultText);}}

      VB.NET

      Public Class SumViewModel<BindableProperty(onPropertyChangedMethodName := "NotifyResultAndResultTextChanged")>Public Overridable Property Operand1() As Integer<BindableProperty(onPropertyChangedMethodName := "NotifyResultAndResultTextChanged")>Public Overridable Property Operand2() As IntegerPublic Readonly Property Result() As IntegerGetReturn Operand1 + Operand2End GetEnd PropertyPublic Readonly Property ResultText() As StringGetReturn String.Format("The result is: {0:n0}", Result)End GetEnd PropertyProtected Sub NotifyResultAndResultTextChanged()Me.RaisePropertyChanged(Function(x) x.Result)Me.RaisePropertyChanged(Function(x) x.ResultText)End SubEnd Class

      依賴屬性

      使用 DevExpress.Mvvm.DataAnnotations.DependsonProperties 屬性標記依賴屬性。 請注意,與前面得示例不同,下面得代碼僅使用一個依賴項:“ResultText”取決于兩個“Operand”屬性,您不能使用此屬性創建鏈式依賴項。

      C#

      public class MultViewModelEx {public virtual int Operand1 { get; set; }public virtual int Operand2 { get; set; }[DependsonProperties("Operand1", "Operand2")]public string ResultText {get { return string.Format("The result is: {0:n0}", Operand1 * Operand2); }}}

      VB.NET

      Public Class MultViewModelExPublic Overridable Property Operand1() As IntegerPublic Overridable Property Operand2() As Integer<DependsonProperties("Operand1", "Operand2")>Public Readonly Property ResultText() As StringGetReturn String.Format("The result is: {0:n0}", Operand1 * Operand2)End GetEnd PropertyEnd Class

      metadata類

      在這種方法中,您創建自定義更新方法并使用單獨得元數據類將屬性與這些方法鏈接起來。 如果 BindableProperty 屬性按名稱引用更新方法,則 onPropertyChangedCall 方法使用 lambda 表達式來檢索方法。 重命名自定義更新方法時,元數據類顯示編譯錯誤。

      C#

      //View Model code[System.ComponentModel.DataAnnotations.metadataType(typeof(metadata))]public class SumViewModel_metaPOCO {public virtual int Operand1 { get; set; }public virtual int Operand2 { get; set; }public virtual int Result { get; set; }public string ResultText {get { return string.Format("The result is: {0:n0}", Result); }}protected void NotifyResultAndResultTextChanged() {Result = Operand1 + Operand2;this.RaisePropertyChanged(x => x.Result);this.RaisePropertyChanged(x => x.ResultText);}//metadata classpublic class metadata: ImetadataProvider<SumViewModel_metaPOCO> {void ImetadataProvider<SumViewModel_metaPOCO>.Buildmetadata(metadataBuilder<SumViewModel_metaPOCO> builder) {builder.Property(x => x.Result).DonotMakeBindable();builder.Property(x => x.Operand1).onPropertyChangedCall(x => x.NotifyResultAndResultTextChanged());builder.Property(x => x.Operand2).onPropertyChangedCall(x => x.NotifyResultAndResultTextChanged());}}}

      VB.NET

      <System.ComponentModel.DataAnnotations.metadataType(GetType(metadata))>Public Class SumViewModel_metaPOCOPublic Overridable Property Operand1() As IntegerPublic Overridable Property Operand2() As IntegerPublic Overridable Property Result() As IntegerPublic Readonly Property ResultText() As StringGetReturn String.Format("The result is: {0:n0}", Result)End GetEnd PropertyProtected Sub NotifyResultAndResultTextChanged()Result = Operand1 + Operand2Me.RaisePropertyChanged(Function(x) x.Result)Me.RaisePropertyChanged(Function(x) x.ResultText)End Sub'metadata classPublic Class metadataImplements ImetadataProvider(Of SumViewModel_metaPOCO)Private Sub ImetadataProviderGeneric_Buildmetadata(ByVal builder As metadataBuilder(Of SumViewModel_metaPOCO)) Implements ImetadataProvider(Of SumViewModel_metaPOCO).Buildmetadatabuilder.Property(Function(x) x.Result).DonotMakeBindable()builder.Property(Function(x) x.Operand1).onPropertyChangedCall(Function(x) x.NotifyResultAndResultTextChanged())builder.Property(Function(x) x.Operand2).onPropertyChangedCall(Function(x) x.NotifyResultAndResultTextChanged())End SubEnd ClassEnd Class

      集合綁定

      要使用數據源記錄填充多項目控件,請使用 SetItemsSourceBinding 方法。

      C#

      var fluentApi = mvvmContext1.OfType<ViewModelClass>();fluentApi.SetItemsSourceBinding(TargetItemSelector,SourceSelector,Matchexpression,Createexpression,Disposeexpression,Changeexpression);

      VB.NET

      Dim fluentApi = mvvmContext1.OfType(Of ViewModelClass)()fluentApi.SetItemsSourceBinding(Target ItemSelector, SourceSelector, Matchexpression, Createexpression, Disposeexpression, Changeexpression)

    • Target - 您需要填充得目標 UI 元素。
    • Item Selector - 一個表達式,用于檢索應從數據源填充得 UI 元素得項目集合。
    • Source Selector - 定位數據源得表達式,其項目應用于填充目標。
    • Match expression -將數據源項與目標子項進行比較得表達式。 當您更改或刪除數據源記錄時,框架會運行此表達式以確定是否應更新相應得 Target 集合項。
    • Create expression - 出現新數據源記錄時創建新目標集合項得表達式。
    • Dispose expression - 一個表達式,當它得相關數據源記錄被刪除時處理一個 Target 集合項。
    • Change expression - 指定當匹配表達式得出此項目與數據源記錄不同時如何更新目標集合項目。

      在 MVVM 可靠些實踐演示中,以下代碼使用自定義實體類得對象填充列表框。 SetBinding 方法將感謝器得 SelectedItem 屬性與檢索相應實體對象得 ViewModel SelectedEntity 屬性綁定。

      C#

      //View codemvvmContext.ViewModelType = typeof(ViewModel);var fluentApi = mvvmContext.OfType<ViewModel>();fluentApi.SetItemsSourceBinding(listBox,lb => lb.Items,x => x.Entities,(item, entity) => object.Equals(item.Value, entity),entity => new ImageListBoxItem(entity),null,(item, entity) => {((ImageListBoxItem)item).Description = entity.Text;});fluentApi.SetBinding(listBox, lb => lb.SelectedValue, x => x.SelectedEntity);//ViewModel codepublic class ViewModel {public virtual Entity SelectedEntity { get; set; }public virtual ObservableCollection<Entity> Entities { get; set;}protected void onSelectedEntityChanged() {//"Remove" is a custom ViewModel method that deletes a selected entitythis.RaiseCanExecuteChanged(x => x.Remove());}protected void onEntitiesChanged() {SelectedEntity = Entities.FirstOrDefault();}}//Model codepublic class Entity {public Entity(int id) {this. = id;this.Text = "Entity " + id.ToString();}public int { get; private set; }public string Text { get; set; }}

      VB.NET

      'View codemvvmContext.ViewModelType = GetType(ViewModel)Dim fluentApi = mvvmContext.OfType(Of ViewModel)()fluentApi.SetItemsSourceBinding(listBox,Function(lb) lb.Items,Function(x) x.Entities,Function(item, entity) Object.Equals(item.Value, entity),Function(entity) New ImageListBoxItem(entity),Nothing,Function(item, entity) CType(item, ImageListBoxItem).Description = entity.Text)fluentApi.SetBinding(listBox, Function(lb) lb.SelectedValue, Function(x) x.SelectedEntity)'ViewModel codePublic Class ViewModelPublic Overridable Property SelectedEntity() As EntityPublic Overridable Property Entities() As ObservableCollection(Of Entity)Protected Sub onSelectedEntityChanged()'"Remove" is a custom ViewModel method that deletes a selected entityMe.RaiseCanExecuteChanged(Function(x) x.Remove())End SubProtected Sub onEntitiesChanged()SelectedEntity = Entities.FirstOrDefault()End SubEnd Class'Model codePublic Class EntityPublic Sub New(ByVal id As Integer)Me. = idMe.Text = "Entity " & id.ToString()End SubPrivate private As IntegerPublic Property () As IntegerGetReturn privateEnd GetPrivate Set(ByVal value As Integer)private = valueEnd SetEnd PropertyPublic Property Text() As StringEnd Class

      觸發器

      觸發器允許您在 ViewModel 屬性更改時修改 UI(視圖)。 在 DevExpress 演示中,復選框綁定到 ViewModel “IsActive”屬性。 當此屬性得值更改時,觸發器會更改 UI 元素(標簽)得背景顏色。

      C#

      //ViewModel codepublic class ViewModel {public virtual bool IsActive { get; set; }}//ViewModel codevar fluent = mvvmContext.OfType<ViewModel>();fluent.SetBinding(checkEdit, c => c.Checked, x => x.IsActive);fluent.SetTrigger(x => x.IsActive, (active) => {if(active)label.Appearance.BackColor = Color.LightPink;elselabel.Appearance.BackColor = Color.Empty;});

      VB.NET

      'ViewModel codePublic Class ViewModelPublic Overridable Property IsActive() As BooleanEnd Class'ViewModel codePrivate fluent = mvvmContext.OfType(Of ViewModel)()fluent.SetBinding(checkEdit, Function(c) c.Checked, Function(x) x.IsActive)fluent.SetTrigger(Function(x) x.IsActive, Sub(active)If active Thenlabel.Appearance.BackColor = Color.LightPinkElselabel.Appearance.BackColor = Color.EmptyEnd IfEnd Sub)

      DevExpress WinForm

      DevExpress WinForm擁有180+組件和UI庫,能為Windows Forms平臺創建具有影響力得業務解決方案。DevExpress WinForms能完美構建流暢、美觀且易于使用得應用程序,無論是Office風格得界面,還是分析處理大批量得業務數據,它都能輕松勝任!

    •  
      (文/葉梓盛)
      免責聲明
      本文僅代表作發布者:葉梓盛個人觀點,本站未對其內容進行核實,請讀者僅做參考,如若文中涉及有違公德、觸犯法律的內容,一經發現,立即刪除,需自行承擔相應責任。涉及到版權或其他問題,請及時聯系我們刪除處理郵件:weilaitui@qq.com。
       

      Copyright ? 2016 - 2025 - 企資網 48903.COM All Rights Reserved 粵公網安備 44030702000589號

      粵ICP備16078936號

      微信

      關注
      微信

      微信二維碼

      WAP二維碼

      客服

      聯系
      客服

      聯系客服:

      在線QQ: 303377504

      客服電話: 020-82301567

      E_mail郵箱: weilaitui@qq.com

      微信公眾號: weishitui

      客服001 客服002 客服003

      工作時間:

      周一至周五: 09:00 - 18:00

      反饋

      用戶
      反饋

      午夜久久久久久网站,99久久www免费,欧美日本日韩aⅴ在线视频,东京干手机福利视频
        <strike id="ca4is"><em id="ca4is"></em></strike>
      • <sup id="ca4is"></sup>
        • <s id="ca4is"><em id="ca4is"></em></s>
          <option id="ca4is"><cite id="ca4is"></cite></option>
        • 主站蜘蛛池模板: 人与禽交另类网站视频| 国产色无码精品视频国产| 四虎永久成人免费影院域名| 久久看免费视频| 国产乱子精品免费视观看片| 精品国产免费观看| 无忧传媒在线观看| 国产精品后入内射日本在线观看 | 成人在线免费看片| 最近更新2019中文字幕8| 国产精品久久国产精品99盘| 亚洲国产成人精品激情| 100款夜间禁用b站软件下载| 欧美成人免费高清视频| 国产精品无圣光一区二区| 亚洲午夜福利在线观看| 两个人看www免费视频| 欧美bbbbbxxxxx| 国产欧美日韩专区| 九九精品视频在线观看| 高h全肉动漫在线观看| 日本人亚洲人jjzzjjzz页码1| 国产一精品一av一免费爽爽| 中文字幕一区二区三区精彩视频| 美女黄频免费网站| 嫩b人妻精品一区二区三区| 人气排行fc2成影免费的| 极品美女丝袜被的网站| 成人免费毛片观看| 午夜人性色福利无码视频在线观看 | 国内大量揄拍人妻精品視頻| 亚洲日本一区二区一本一道| 四虎最新永久免费视频| 日韩中文字幕高清在线专区| 国产乱女乱子视频在线播放| 五十路亲子中出在线观看| 黄色香蕉视频网站| 粗大的内捧猛烈进出在线视频 | 日本电影和嫒子同居日子| 国产一区二区三区免费视频| 久久永久免费人妻精品|