Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t将学生列表中的项目添加到另一个班级的列表框中_C#_Wpf_Listbox - Fatal编程技术网

C# Can';t将学生列表中的项目添加到另一个班级的列表框中

C# Can';t将学生列表中的项目添加到另一个班级的列表框中,c#,wpf,listbox,C#,Wpf,Listbox,我在将学生列表中的项目显示到窗口的列表框时遇到问题。 我希望我的列表框能够在单击按钮时显示所有学生的姓名 我的问题是,显示项的方法位于不同的类中,无法识别ListBox名称 这是我在main window.cs中的代码: using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using

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

我的问题是,显示项的方法位于不同的类中,无法识别ListBox名称

这是我在
main window.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();
        }
    }
}
这是我在大学课堂上的代码:

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());
            }
        }
    }
}
我能够使用
MessageBox.Show()
测试列表,因此我知道信息就在那里,我似乎无法将其显示到我的列表框中


谢谢任何能帮忙的人

您可以将学生列表公开,并在
主窗口中填充列表框,如下所示

警告未测试代码

在大学课堂上

    public List<Student> student = new List<Student>();
    // 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();
    }

您可以将您的学生列表公开,并在
主窗口中填充列表框,如下所示

警告未测试代码

在大学课堂上

    public List<Student> student = new List<Student>();
    // 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();
    }

处理列表框不是大学的任务。您可以简单地声明为:

class University
{
    public List<Student> Students { get; } = new List<Student>();
}
现在,您可以添加具有

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;
    }
}

但是,支持字段是隐藏的,无法直接访问。

处理列表框不是
大学的任务。您可以简单地声明为:

class University
{
    public List<Student> Students { get; } = new List<Student>();
}
现在,您可以添加具有

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;
    }
}

但是,备份字段是隐藏的,无法直接访问。

视图
控制器
()分离
视图
控制器
()