【问题标题】:Binding array of struct to the ToolStripCombobox将结构数组绑定到 ToolStripCombobox
【发布时间】:2010-07-07 18:48:30
【问题描述】:

我正在尝试将结构数组绑定到 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。我做错了什么?

【问题讨论】:

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


    【解决方案1】:

    您的成员应该是属性才能进行绑定。

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

    【讨论】:

      猜你喜欢
      • 2018-09-15
      • 2014-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 1970-01-01
      • 2015-11-19
      相关资源
      最近更新 更多