2011-04-28 14 views
2

我試圖通過INotifyPropertyChanged實現更新textedit控件通過客戶端類對象數據綁定,我無法讓它工作。 (數據源)後面的對象更新但textedit仍保留空白。如果我輸入文本到編輯框中,數據源被更新。請你幫忙嗎?以下是我正在使用的相關代碼:無法正確實現對象的屬性值和控件的屬性值之間的綁定

public class Client : NotifyProperyChangedBase 
{ 

    private string _firstname; 
    public string Firstname 
    { 
     get 
     { 
      return this._firstname; 
     } 
     set 
     { 
      this.CheckPropertyChanged<string>("Firstname", ref _firstname, ref value); 
     } 
    } 
} 


public Client ClientA = new Client(); 

Binding fname = new Binding("Text", ClientA, "Firstname", true, DataSourceUpdateMode.OnPropertyChanged); 

ultraTextEditor_firstname.DataBindings.Add(fname); 

ClientA.Firstname =「testN」; < ==編輯框保持空白...

我在這裏錯過了什麼嗎?在此先感謝彼得。

+0

「WinForm INotifyPropertyChanged不起作用」我非常懷疑。 – 2011-04-28 23:25:52

回答

0

我假設你的基地沿着this example的方向執行。如果我的假設不正確,則需要提供NotifyProperyChangedBase類的實現。

您可能還想查看Binding(String, Object, String, Boolean, DataSourceUpdateMode)構造函數文檔,因爲它討論了綁定嘗試查找的控件事件。

望着這個例子中,你會想嘗試這樣的事:

System.ComponentModel.BindingList<Client> bindings = new System.ComponentModel.BindingList<Client>(); 

Client clientA = bindings.AddNew(); 
clientA.Firstname = "John"; 

textEditControl.DataSource = bindings; 

// This change presumably will be refelected in control 
clientA.Firstname = "Jane"; 

更新: 審查關於ControlBindingsCollection類的Add方法的文檔後,我相信Binding的數據源需要實現IListSource接口才能正確參與綁定(所有MSDN示例都是實現此接口的DataSet或DataTable)。

+0

我認爲'CheckPropertyChanged ()'引發了事件,所以不需要再次提升。也就是說,如果OP使用這個帖子中的類http://www.codeproject.com/KB/cs/BindBetterINotifyProperty.aspx?display=Print – 2011-04-28 23:55:13

+0

嗯,你是對的。 – Oppositional 2011-04-28 23:59:12

+0

感謝您的幫助。我稍後會嘗試。 – 2011-04-29 07:46:42