2012-05-15 18 views
0

我有一個名爲PlatypusInfo的類。我在綁定類時做了什麼錯誤?

我調用返回的類的實例的方法:

PlatypusInfo pi; 
. . . 
pi = MammalData.PopulateplatypusData(oracleConnectionMainForm, textBoxPlatypusID.Text); 
. . . 
public static PlatypusInfo PopulateplatypusData(OracleConnection oc, String platypusID) { 

    int platypusABCID = getABCIDForDuckBillID(oc, platypusID); 
    platypusInfo pi = new platypusInfo(); 

...但得到這個錯誤信息:「System.ArgumentException是未處理 消息=無法綁定到屬性或列platypusName在數據源 參數名稱:數據成員 來源= System.Windows.Forms的 PARAMNAME =數據成員」

...在這行代碼:

textBoxPlatypusID.DataBindings.Add(new Binding("Text", pi, "platypusName")); 

我在想,用我的代碼,PlatypusInfo類(它的實例是「pi」)的platypusName成員應該被分配給textBoxPlatypusID的Text屬性。

所以,我理解錯誤,我是否錯了,或兩者?

+3

PlatypusInfo類是否有一個名爲'platypusName'類型爲string的公共屬性? –

+0

你能張貼'PlatypusInfo' – SwDevMan81

+0

@約翰代碼:不完全是一個屬性: 公共類PlatypusInfo { \t \t公共字符串PlatypusName; –

回答

1

你需要把它從一個字段更改爲一個屬性並添加INotifyPropertyChanged接口的實現。因此,像這樣:

public class PlatypusInfo : INotifyPropertyChanged 
{   
    public event PropertyChangedEventHandler PropertyChanged; 
    private String _PlatypusName; 

    public String PlatypusName 
    { 
     get 
     { 
      return _PlatypusName; 
     } 
     set 
     { 
      _PlatypusName = value; 
      NotifyPropertyChanged("PlatypusName"); 
     } 
    } 

    private void NotifyPropertyChanged(String info) 
    { 
     PropertyChangedEventHandler property_changed = PropertyChanged; 
     if (property_changed != null) 
     { 
      property_changed(this, new PropertyChangedEventArgs(info)); 
     } 
    } 
} 

然後綁定應該是這樣的:

textBoxPlatypusID.DataBindings.Add(new Binding("Text", pi, "PlatypusName")); 

假設piPlatypusInfo對象。

0

請問類PlatypusInfo實現接口INotifyPropertyChanged的

+0

不,必須嗎?如果是的話,我會google的例子... –

+0

當你使用數據綁定時,你必須實現INotifyPropertyChanged,並且你需要使用ObservableCollection的集合 –

+0

如果你希望你的UI在後臺更新時你只需要實現這些接口數據變化。無論如何,綁定都可以工作 –