我的目標是將新創建的學生參數從TextBox添加到List集合中。
據我所知,下面的代碼不這樣做。將WPF文本框中的值添加到列表
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
btnCreateStudent.Click += btnCreateStudent_Click;
}
private void btnCreateStudent_Click(object sender, RoutedEventArgs e)
{
Student student = new Student();
student.Name = txtFirstName.Text;
student.Surname = txtLastName.Text;
student.City = txtCity.Text;
student.Students.Add(student);
txtFirstName.Text = "";
txtLastName.Text = "";
txtCity.Text = "";
}
class Student
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string surname;
public string Surname
{
get { return surname; }
set { surname = value; }
}
private string city;
public string City
{
get { return city; }
set { city = value; }
}
public List<Student> Students = new List<Student>();
}
}
一個'List'或'ListBox'? –
一個列表。我需要它來存儲表單用戶輸入的數據,以便稍後用戶在表單中按下「Prevoius」和「Next」按鈕時在TextBox中顯示。 – Belkin