0

我无法使用存储在不同类中的信息填充列表框。此类称为 TeacherTools,其中使用了许多方法来控制该列表中的学生对象。

我要做的基本上是将学生的名字添加到列表中,但我不确定语法。

public partial class TeacherTools : Window
{
    private List<Student> studentList = new List<Student>();

    public List<Student> Kids
    {
       get { return studentList; }
       set { studentList = value; }
    }
}

public partial class MainWindow : Window
{
    private void beginTest(object sender, RoutedEventArgs e)
    {
        foreach (Student s in Kids) //Unsure here
        {
            studentsList.Items.Add(s.Name);
        }
    }
}

感谢您的时间

4

1 回答 1

2

我想你想要做的是在列表框中显示学生的名字..

studentsList.DataSource = teachTool.Kids;
studentsList.DisplayMember = "Name";

这里有一个更长的例子 http://msdn.microsoft.com/en-GB/library/system.windows.forms.listcontrol.displaymember.aspx

编辑:这样做将给予与学生类相关的更多控制自由编辑2:以上是针对winforms的......ItemSource并且DisplayMemberPath似乎是wpf等价物

DisplayMemberPath ItemSource

于 2013-04-13T20:14:40.440 回答