为什么我得到“无法绑定到新的显示成员”使用此代码

本文关键字:成员 显示 代码 绑定 为什么 | 更新日期: 2023-09-27 18:33:58

我有以下代码将字典的值分配给组合框:

private void PopulateComboBoxWithSchedulableWeeks()
{
    int WEEKS_TO_OFFER_COUNT = 13;
    BindingSource bs = new BindingSource();
    Dictionary<String, DateTime> schedulableWeeks = 
GetWeekBeginningsDict(WEEKS_TO_OFFER_COUNT);
    bs.DataSource = schedulableWeeks;
    comboBoxWeekToSchedule.DataSource = bs;
    comboBoxWeekToSchedule.DisplayMember = "Key";
    comboBoxWeekToSchedule.ValueMember = "Value";
}
public static Dictionary<String, DateTime> GetWeekBeginningsDict(int 
    countOfWeeks)
{
    DateTime today = DateTime.Today;
    int daysUntilMonday = ((int)DayOfWeek.Monday - (int)today.DayOfWeek + 7) 
        % 7;
    DateTime nextMonday = today.AddDays(daysUntilMonday);
    Dictionary<String, DateTime> mondays = new Dictionary<String, DateTime>
();
    if (!IsAssemblyOrConventionWeek(nextMonday))
    {
        mondays.Add(nextMonday.ToLongDateString(), nextMonday);
    }
    for (int i = 0; i < countOfWeeks; i++)
    {
        nextMonday = nextMonday.AddDays(7);
        if (!IsAssemblyOrConventionWeek(nextMonday))
        {
            mondays.Add(nextMonday.ToLongDateString(), nextMonday);
        }
    }
    return mondays;
}

在运行时,我得到"无法绑定到新的显示成员",但是,在以下行中使用以下代码:

comboBoxWeekToSchedule.ValueMember = "Value";

为什么?

为什么我得到“无法绑定到新的显示成员”使用此代码

不能将ComboBox绑定到字段。在定义 DisplayMember 和 DisplayValue 后尝试加载数据源:

comboBoxWeekToSchedule.DisplayMember = "Key";
comboBoxWeekToSchedule.ValueMember = "Value";
comboBoxWeekToSchedule.DataSource = bs;

增加组合框的大小,我敢打赌它说System.Collections.Generic.Dictionary...但我怀疑你知道这个线程的答案。你到底生活在什么样的回声中?