【发布时间】:2014-07-31 19:45:10
【问题描述】:
我有一个“所有选择”的列表框和一个对象,其中所有选择的选项都为 0(因此具有多值选择模式的列表框)。我需要选择在该列表框中选择的所有对象的选项。
所以我将 ListBox.Datasource 绑定到所有可用选项的列表,并尝试找到一种方法将该对象选项绑定到 Listbox SelectedItems 属性,但我没有成功找到有关如何执行此操作的任何信息。
示例
假设我们有 3 个表:学生、课程和学生课程。因此,在学生的表格中,我需要一个包含所有可用课程的列表框,并在该列表中选择他的学生课程表中的所有学生课程。
是否可以使用数据绑定来获取此信息?
我尝试的方式
//1. getting the available list
List.DataSource = ..the list of all the courses
List.DisplayMember = "Name";
List.ValueMember = "Id";
//2. selecting the appropriate items in the list
List.SelectedItems.Clear();
foreach (var c in student.StudentsCourses)
{
//in this strange case Id is equal to the index in the list...
List.SetSelected(c.CourseId, true);
}
//instead of this "2." part I was hoping to use something like this:
List.DataBindings.Add("SelectedItems", student.StudentsCourses, "CourseId");
但是当我尝试这样做时出现错误:无法绑定到属性“SelectedItems”,因为它是只读的
【问题讨论】:
标签: c# winforms data-binding listbox multi-select