【问题标题】:Why is my combobox item not being discovered and selected?为什么我的组合框项目没有被发现和选择?
【发布时间】:2016-02-21 18:10:09
【问题描述】:

受到here 答案的启发,我重构了我的代码,在通过 LINQ 初始填充后将一条记录添加到我的通用列表中,然后尝试在组合框中选择该添加的项目,该组合框中分配了列表的内容。

对于某些上下文/概念化,组合框会填充候选学生;但是,如果显示的那一周已经安排好了,那么已经安排在该槽中的那一周的学生将被插入到列表中,该列表在其他列表之后用作组合框的数据源。最后,如果组合框中确实存在这样的学生,我的意图是选择那个(但如果需要更改分配的学生,其他学生仍保留在列表中)。

代码如下:

private void PopulateBibleReadingComboBox()
{
    int BIBLE_READING_TALK_TYPE = 1;
    if (!System.IO.File.Exists(AYttFMConstsAndUtils.STUDENTS_FILENAME)) return;
    if (null == studentsList) return;
    string assignedStudentFirstname = string.Empty;
    string assignedStudentLastname = string.Empty;
    Student assignedStudent = null;

    if (currentWeekSaved)
    {
        DateTime currentWeek = Convert.ToDateTime(comboBoxWeekToSchedule.SelectedValue);
        AssignmentHistory ah = AYttFMConstsAndUtils.AssignmentHistList
            .FirstOrDefault(i => i.WeekOfAssignment == currentWeek && i.TalkType == 1);
        assignedStudentFirstname = AYttFMConstsAndUtils.GetStudentFirstNameForID(ah.StudentID_FK);
        assignedStudentLastname = AYttFMConstsAndUtils.GetStudentLastNameForID(ah.StudentID_FK);
        assignedStudent = new Student() {FirstName = assignedStudentFirstname, LastName = assignedStudentLastname, StudentID = ah.StudentID_FK};
    }
    List<Student> BRStudents =
    studentsList.Where(h => h.EnrolledInAYttFM)
        .Where(i => i.RecommendedNextTalkTypeID.Equals(BIBLE_READING_TALK_TYPE))
        .OrderBy(j => j.WeekOfLastAssignment)
        .ToList();
    if (null != assignedStudent)
    {
        BRStudents.Add(assignedStudent);
    }
    comboBoxBR.DataSource = BRStudents;
    comboBoxBR.DisplayMember = "FullName";
    comboBoxBR.ValueMember = "StudentID";
    if (null != assignedStudent))
    {
        comboBoxBR.SelectedIndex = comboBoxBR.Items.IndexOf(assignedStudent.FullName);
    }
}

问题在于,虽然条件“SelectedIndex/IndexOf”行到达,并且assignedStudent.FullName是它应该是的,而现在添加到列表中然后是组合框,not 用该行选择了该项目:

comboBoxBR.SelectedIndex = comboBoxBR.Items.IndexOf(assignedStudent.FullName);

更确切地说,comboBoxBR.SelectedIndex 为 -1(尽管此时的组合框中确实存在 fullName)。

注意:Student 类的“FullName”成员是计算出来的:

public class Student
{
    public int StudentID { get; set; }
    . . .
    public string FirstName { get; set; }
    public string LastName { get; set; }
    . . .
    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", FirstName, LastName);
        }
        set { ; } 
    }
}

【问题讨论】:

    标签: c# linq data-binding combobox generic-list


    【解决方案1】:

    列表由 Student 对象组成,但 FullName 是一个字符串。您需要查找整个 Student 对象。您需要将相关行更改为:

    comboBoxBR.SelectedIndex = comboBoxBR.Items.IndexOf(assignedStudent);
    

    【讨论】:

      【解决方案2】:

      当您使用DataSource 时,就好像对象已被添加到集合中(并且以某种形式或方式很可能是引擎盖下的情况)。因此,仅在 items 集合中搜索名称将失败。更多 MCVE:

      Students = new List<Student>();
      
      Students.Add(new Student(7, "Zowie", "Halston"));
      Students.Add(new Student(6, "Ziggy", "Watson"));
      Students.Add(new Student(18, "Zalgo", "d'Artagnan"));
      Students.Add(new Student(67, "Tabitha", "Black"));
      
      Student luckyStudent = Students.First(w => w.FirstName == "Ziggy");
      
      cbo1.DataSource = Students;
      cbo1.DisplayMember = "FullName";
      cbo1.ValueMember = "Id";
      

      然后,设置选择:

      if (luckyStudent != null)
      { 
          // set selected: (WORKS):
          //cbo1.SelectedItem = luckyStudent;
      
          // set Index of item (WORKS):
          //cbo1.SelectedIndex = cbo1.Items.IndexOf(luckyStudent);
      
          // set Index of item name (FAILS):
          cbo1.SelectedIndex = cbo1.Items.IndexOf(luckyStudent.FullName);
      }   
      

      一般来说,当使用DataSource 时,我会尽量避免摆弄项目集合。如果您尝试从items 添加或删除,您会被骂。所以,为此,我会使用

      cbo1.SelectedItem = luckyStudent;
      

      您可以在集合中找到它们,但它会帮助您忘记控件已绑定。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多