C# 如何绑定WinForm';s listbox selecteditems

C# 如何绑定WinForm';s listbox selecteditems,c#,winforms,data-binding,listbox,multi-select,C#,Winforms,Data Binding,Listbox,Multi Select,我有一个包含“所有选项”的列表框和一个选择了0到所有选项的对象(因此具有多值selectionmode的列表框)。我需要选择该列表框中所选对象的所有选项 因此,我将ListBox.Datasource绑定到所有可用选项的列表中,并试图找到一种方法将该对象选项绑定到ListBox SelectedItems属性,但没有找到任何有关如何执行此操作的信息 例子 假设我们有三张桌子:学生、课程和学生。因此,在学生表格中,我需要有一个包含所有可用课程的列表框,并在该列表中选择他学生资源表中的所有学生课程

我有一个包含“所有选项”的列表框和一个选择了0到所有选项的对象(因此具有多值selectionmode的列表框)。我需要选择该列表框中所选对象的所有选项

因此,我将ListBox.Datasource绑定到所有可用选项的列表中,并试图找到一种方法将该对象选项绑定到ListBox SelectedItems属性,但没有找到任何有关如何执行此操作的信息

例子 假设我们有三张桌子:学生、课程和学生。因此,在学生表格中,我需要有一个包含所有可用课程的列表框,并在该列表中选择他学生资源表中的所有学生课程

是否可以使用数据绑定获取此信息?

我尝试的方式
但是当我尝试这样做时,我得到了一个错误:无法绑定到属性'SelectedItems',因为它是只读的

我不确定我是否正确,但是如果我正确,是的,您可以这样做

例如:

List<KeyValuePair<string, Course>> coursesList = new List<KeyValuePair<string, Course>>();
List<Course> cList = // Get your list of courses

foreach (Course crs in cList)
{
    KeyValuePair<string, Course> kvp = new KeyValuePair<string, Course>(crs.Name, crs);
    cList.Add(kvp);
}

// Set display member and value member for your listbox as well as your datasource
listBox1.DataSource = coursesList;
listBox1.DisplayMember = "Key"; // First value of pair as display member
listBox1.ValueMember = "Value"; // Second value of pair as value behind the display member

var studentsList = // Get your list of students somehow

foreach (Student student in studentsList)
{
    foreach (KeyValuePair<string, Course> item in listBox1.Items)
    {
        // If students course is value member in listBox, add it to selected items
        if (student.Course == item.Value)
            listBox1.SelectedItems.Add(item);
    }
}
List coursesList=new List();
List cList=//获取课程列表
foreach(cList中的课程crs)
{
KeyValuePair kvp=新的KeyValuePair(crs.Name,crs);
cList.Add(kvp);
}
//为列表框和数据源设置显示成员和值成员
listBox1.DataSource=课程列表;
listBox1.DisplayMember=“Key”//作为显示成员的对的第一个值
listBox1.ValueMember=“Value”//成对的第二个值作为显示成员后面的值
var studentsList=//以某种方式获取学生列表
foreach(学生名单中的学生)
{
foreach(listBox1.Items中的KeyValuePair项)
{
//如果学生课程是列表框中的值成员,请将其添加到所选项目中
if(student.Course==item.Value)
列表框1.选择编辑项。添加(项);
}
}

希望你能理解其中的逻辑。既然你没有密码,我也帮不了你。干杯

我不确定我是否对了你,但如果我对了,是的,你可以做到

例如:

List<KeyValuePair<string, Course>> coursesList = new List<KeyValuePair<string, Course>>();
List<Course> cList = // Get your list of courses

foreach (Course crs in cList)
{
    KeyValuePair<string, Course> kvp = new KeyValuePair<string, Course>(crs.Name, crs);
    cList.Add(kvp);
}

// Set display member and value member for your listbox as well as your datasource
listBox1.DataSource = coursesList;
listBox1.DisplayMember = "Key"; // First value of pair as display member
listBox1.ValueMember = "Value"; // Second value of pair as value behind the display member

var studentsList = // Get your list of students somehow

foreach (Student student in studentsList)
{
    foreach (KeyValuePair<string, Course> item in listBox1.Items)
    {
        // If students course is value member in listBox, add it to selected items
        if (student.Course == item.Value)
            listBox1.SelectedItems.Add(item);
    }
}
List coursesList=new List();
List cList=//获取课程列表
foreach(cList中的课程crs)
{
KeyValuePair kvp=新的KeyValuePair(crs.Name,crs);
cList.Add(kvp);
}
//为列表框和数据源设置显示成员和值成员
listBox1.DataSource=课程列表;
listBox1.DisplayMember=“Key”//作为显示成员的对的第一个值
listBox1.ValueMember=“Value”//成对的第二个值作为显示成员后面的值
var studentsList=//以某种方式获取学生列表
foreach(学生名单中的学生)
{
foreach(listBox1.Items中的KeyValuePair项)
{
//如果学生课程是列表框中的值成员,请将其添加到所选项目中
if(student.Course==item.Value)
列表框1.选择编辑项。添加(项);
}
}

希望你能理解其中的逻辑。既然你没有密码,我也帮不了你。干杯

我不确定我是否对了你,但如果我对了,是的,你可以做到

例如:

List<KeyValuePair<string, Course>> coursesList = new List<KeyValuePair<string, Course>>();
List<Course> cList = // Get your list of courses

foreach (Course crs in cList)
{
    KeyValuePair<string, Course> kvp = new KeyValuePair<string, Course>(crs.Name, crs);
    cList.Add(kvp);
}

// Set display member and value member for your listbox as well as your datasource
listBox1.DataSource = coursesList;
listBox1.DisplayMember = "Key"; // First value of pair as display member
listBox1.ValueMember = "Value"; // Second value of pair as value behind the display member

var studentsList = // Get your list of students somehow

foreach (Student student in studentsList)
{
    foreach (KeyValuePair<string, Course> item in listBox1.Items)
    {
        // If students course is value member in listBox, add it to selected items
        if (student.Course == item.Value)
            listBox1.SelectedItems.Add(item);
    }
}
List coursesList=new List();
List cList=//获取课程列表
foreach(cList中的课程crs)
{
KeyValuePair kvp=新的KeyValuePair(crs.Name,crs);
cList.Add(kvp);
}
//为列表框和数据源设置显示成员和值成员
listBox1.DataSource=课程列表;
listBox1.DisplayMember=“Key”//作为显示成员的对的第一个值
listBox1.ValueMember=“Value”//成对的第二个值作为显示成员后面的值
var studentsList=//以某种方式获取学生列表
foreach(学生名单中的学生)
{
foreach(listBox1.Items中的KeyValuePair项)
{
//如果学生课程是列表框中的值成员,请将其添加到所选项目中
if(student.Course==item.Value)
列表框1.选择编辑项。添加(项);
}
}

希望你能理解其中的逻辑。既然你没有密码,我也帮不了你。干杯

我不确定我是否对了你,但如果我对了,是的,你可以做到

例如:

List<KeyValuePair<string, Course>> coursesList = new List<KeyValuePair<string, Course>>();
List<Course> cList = // Get your list of courses

foreach (Course crs in cList)
{
    KeyValuePair<string, Course> kvp = new KeyValuePair<string, Course>(crs.Name, crs);
    cList.Add(kvp);
}

// Set display member and value member for your listbox as well as your datasource
listBox1.DataSource = coursesList;
listBox1.DisplayMember = "Key"; // First value of pair as display member
listBox1.ValueMember = "Value"; // Second value of pair as value behind the display member

var studentsList = // Get your list of students somehow

foreach (Student student in studentsList)
{
    foreach (KeyValuePair<string, Course> item in listBox1.Items)
    {
        // If students course is value member in listBox, add it to selected items
        if (student.Course == item.Value)
            listBox1.SelectedItems.Add(item);
    }
}
List coursesList=new List();
List cList=//获取课程列表
foreach(cList中的课程crs)
{
KeyValuePair kvp=新的KeyValuePair(crs.Name,crs);
cList.Add(kvp);
}
//为列表框和数据源设置显示成员和值成员
listBox1.DataSource=课程列表;
listBox1.DisplayMember=“Key”//作为显示成员的对的第一个值
listBox1.ValueMember=“Value”//成对的第二个值作为显示成员后面的值
var studentsList=//以某种方式获取学生列表
foreach(学生名单中的学生)
{
foreach(listBox1.Items中的KeyValuePair项)
{
//如果学生课程是列表框中的值成员,请将其添加到所选项目中
if(student.Course==item.Value)
列表框1.选择编辑项。添加(项);
}
}

希望你能理解其中的逻辑。既然你没有密码,我也帮不了你。干杯

@prokursor你成功了吗?我是在我的随机应用程序上这样做的。我是以你建议的类似方式做的,但我不知怎么想,也许有一种更简单的方法,比如使用selecteditems的绑定,诸如此类…@Prokuros我不这么认为,但你可以总是希望有人能给出一个简单的答案。有时候,没有简单的方法来做事情=Pyour right:)我只是尽可能地让代码更小-这就是为什么我尽可能地寻找更简单的解决方案,但在这种情况下,似乎没有为selecteditems进行数据绑定的可能性。我猜我的解决方案可能会产生意想不到的结果(比如选择的项目比需要的多),但您的似乎工作正常-因此我接受您的解决方案作为答案-谢谢您的帮助!@prokursor您使它正常工作了吗?我在随机应用程序中这样做。我做到了