2010-07-07 94 views
0

我試圖將結構數組綁定到ToolStripCombobox,但沒有成功。將結構數組綁定到ToolStripCombobox

我試過使用它,就像在this的例子中,但是當我嘗試設置一個值成員時出現錯誤。

我的代碼如下所示:

public struct PlayTimeLength 
{ 
    public string Description; 
    public double Seconds; 
    public PlayTimeLength(string description, double seconds) 
    { 
     Description = description; 
     Seconds = seconds; 
    } 
} 

    public PlayTimeLength[] PlayTimeLengths = {new PlayTimeLength("1 minuta", 1*60), new PlayTimeLength("3 minuty", 3*60), new PlayTimeLength("5 minut", 5*60)}; 

和實際的綁定代碼:

 cbxTimes.ComboBox.DataSource = PlayTimeLengths; 
     cbxTimes.ComboBox.DisplayMember = "Description"; 
     cbxTimes.ComboBox.ValueMember = "Seconds"; //<-- exception here 

cbxTimesToolStripCombobox類型。我究竟做錯了什麼?

回答

0

您的會員應該是屬性才能進行綁定。

private string description; 
public string Description 
{ 
    get 
    { 
     return description; 
    } 
    set 
    { 
     description = value; 
    } 
} 
private double seconds; 
public double Seconds 
{ 
    get 
    { 
     return seconds; 
    } 
    set 
    { 
     seconds = value; 
    } 
}