无法将学生列表中的项目添加到另一个 class 的列表框

Can't add items from a List of Students to a ListBox from another class

我在 Window 上的列表框中显示学生列表中的项目时遇到问题。
我希望我的列表框能够在单击按钮时显示所有学生的姓名。

我的问题是显示项目的方法是不同的 class 并且无法识别 ListBox 名称。

这是我在 MainWindow.cs 中的代码:

using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UniRecords
{
    public partial class MainWindow : Window
    {
        //allows for communication to university class
        private University myUniversity = new University();

        public MainWindow()
        {
            InitializeComponent();
        }

        //Add to List Button
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //adding one student 
            Student peter = new Student();
            peter.Name = "Peter";
            peter.Address = "3 braeburn Drive";
            peter.Matriculation = 43425321;

            //add student through 3 different text boxes
            Student newStudent = new Student(txtName.Text, txtAddress.Text, Convert.ToInt32(this.txtMatriculation.Text));

            //communicates with addStudent Method in University class
            myUniversity.addStudent(newStudent);
        }

        //Display in list box button
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //communicates with showClassList method in university class
            myUniversity.showClassList();
        }
    }
}

这是我在大学时的代码Class:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace UniRecords
{
    class University
    {
        //Initiating list to store students
        private List<Student> student = new List<Student>();

        public void addStudent(Student newStudent)
        {
            //adding student to list
            student.Add(newStudent);
        }

        public void showClassList() //Links to Student class
        {
            foreach (Student c in student)
            {
                //listStudent is the name of the listbox
                //c.showList communicates with student class and return value for students name
                //THIS IS WHERE I GET LOST
                listStudent.Items.Add(c.showList());
            }
        }
    }
}

这是我在 Student Class 中的代码:

using System.Collections.Generic;
using System.Windows;

namespace UniRecords
{
    class Student
    {
        private string name;
        private string address;
        private string dob;
        private int matricNo;

        public Student() { }

        //constructor to take in 3 arguments
        public Student(string studentName, string studentAddress, int studentMatricNo)
        {
           name = studentName;
           address = studentAddress;
           matricNo = studentMatricNo;
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public string Address
        {
            get
            {
                return address;
            }
            set
            {
                address = value;
            }
        }

        public int Matriculation
        {
            get
            {
                return matricNo;
            }
            set
            {
                matricNo = value;
            }
        }

        public string showList()
        {
            return name + "\n";
        }
    }
}

我能够使用 MessageBox.Show() 测试列表,所以我知道信息就在那里,我似乎无法将它显示到我的列表框。

谢谢任何能提供帮助的人!

您可以制作学生列表 public 并填充 MainWindow class 中的列表框,如下所示。

警告未经测试的代码

在大学里Class

    public List<Student> student = new List<Student>();

在主窗口中Class

    // Moved from University Class
    private void showClassList() //Links to Student class
    {
        foreach (Student c in myUniversity.student)
        {
            //listStudent is the name of the listbox
            //c.showList communicates with student class and return value for students name
            listStudent.Items.Add(c.showList());
        }
    }
    //Display in list box button
    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        //communicates with showClassList method in university class
        showClassList();
    }

University class 的任务不是处理列表框。您可以简单地将其声明为:

class University
{
    public List<Student> Students { get; } = new List<Student>();
}

如果覆盖学生的ToString方法,则可以直接将学生添加到列表框中。列表框自动使用 ToString 来显示项目。这允许您将 Student 个对象而不是字符串添加到列表框中。

class Student
{
    public Student(string studentName, string studentAddress, int studentMatricNo)
    {
        Name = studentName;
        Address = studentAddress;
        Matriculation = studentMatricNo;
    }

    public string Name { get; set; }
    public string Address { get; set; }
    public int Matriculation { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

现在您可以添加学生

private void Button_Click_1(object sender, EventArgs e)
{
    Student newStudent = new Student(txtName.Text, txtAddress.Text, Convert.ToInt32(this.txtMatriculation.Text));
    myUniversity.Students.Add(newStudent);
}

你可以用

显示列表
private void Button_Click_2(object sender, EventArgs e)
{
    listStudent.Items.Clear();
    listStudent.Items.AddRange(myUniversity.Students.ToArray());
}

请注意,我使用了自动实现的属性。他们自动创建和处理他们的支持领域。例如

public string Name { get; set; }

完全一样
private string name;

public string Name
{
    get
    {
        return name;
    }
    set
    {
        name = value;
    }
}

但是,支持字段是隐藏的,不能直接访问。