2012-12-18 23 views
2

我似乎無法弄清楚這一點,並找不到任何答案。強制BindingSource更新數據/型號

我有一個Combobox綁定到我的模型中的屬性。 我只是複製並粘貼在我的代碼鍵行:

 this.m_typeCombobox.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.m_bindingSource, "Type", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); 

我的模型:

public class TypeConfig : INotifyPropertyChanged 
{  
      public event PropertyChangedEventHandler PropertyChanged; 
      private EnumType<eLType> m_type; 
    public EnumType<eLType> Type 
    { 
     get { return m_type; } 
     set 
     { 
      if (m_type!= value) 
      { 
       m_type= value; 
       var handler = PropertyChanged; 
       if (handler != null) 
        handler(this, new PropertyChangedEventArgs("Type")); 
      } 
     } 
    } 

我需要有更新的組合框EditValueChanged事件的模型,但它看起來像模型更新後來。 EditValueChanged是更改時最新調用的事件。

我已經試過這樣:

void m_TypeCombobox_EditValueChanged(object sender, EventArgs e) 
    { 
      m_bindingSource.EndEdit(); //this doesn't work 
      //need to have the new value here 
    } 

這裏的MSDN說什麼:

當EndEdit中的方法被調用時,所有待處理的更改應用到底層數據源。 除非數據源包含的對象實現IEditableObject接口,否則此方法無效。如果對象沒有實現IEditableObject接口,則在每次更改後立即將對數據的更改複製到基礎數據源。

因此,從我的非公開模型中更改組合框值時應立即更新模型。

我使用的DevExpress組合框幾乎與普通的WinForms組合框相同。

我該如何解決這個問題?

+1

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

0

嘗試做結合「值」屬性,而不是「的EditValue」 我希望它能幫助你

+0

看起來像這種類型DevExpress.XtraEditors.ComboBoxEdit不提供Value屬性。我必須使用Editvalue。 –

0

對於BindingSource.EndEdit做任何事情,你需要實現System.ComponentModel.IEditableObject所包含的項目在BindingSource中。

當您在綁定源上調用「EndEdit」時,它隨後會調用其實現IEditableObject的列表中的項的相應IEditableObject.EndEdit()方法。如上所述,我遇到了一些與EndEdit有關的問題,例如,用戶關閉表單時,並未針對所有BeginEdit被調用的項目調用EndEdit。