将struct的数组绑定到ToolStripCombobox

时间:2010-07-07 18:48:31

标签: winforms data-binding c#-3.0 combobox

我正在尝试将结构数组绑定到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

cbxTimes的类型为ToolStripCombobox。我做错了什么?

1 个答案:

答案 0 :(得分: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;
    }
 }