简体   繁体   English

如何遍历列表中对象的属性?

[英]How to iterate through the properties of the objects in a list?

I have a list of objects and i want to iterate through the properties of the object using the 'previous' and 'next' buttons in WPF. 我有一个对象列表,我想使用WPF中的“上一个”和“下一个”按钮迭代对象的属性。
For example if i'm iterating through the properties of an object then by clicking the 'previous' button i encounter the properties of the object located before the object i was currently iterating through and same case for the 'next' button ... 例如,如果我正在遍历对象的属性,那么通过单击“上一个”按钮,我会遇到位于我当前正在迭代的对象之前的对象的属性,以及“下一个”按钮的相同情况......

if i have a Student class containing properties first and last name and number of student objects stored in studentList then how would i do that? 如果我有一个Student类,其中包含存储在studentList中的属性的名字和姓氏以及学生对象的数量,那我该怎么做?
I am only concerned about the implementation of the eventHandler method for 'next' and 'previous' buttons 我只关心'next'和'previous'按钮的eventHandler方法的实现

class Student
{
    private string firstName;

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }
    private string lastName;

    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }
    private string city;

    public string City
    {
        get { return city; }
        set { city = value; }
    }

}

private void btnCreateStudent_Click(object sender, RoutedEventArgs e)
    {
        Student student1 = new Student();
        student1.FirstName = txtFirstName.Text;
        student1.LastName = txtLastName.Text;
        student1.City = txtCity.Text;

        studentList.Add(student1);

        MessageBox.Show("Student Created");

        txtFirstName.Clear();
        txtLastName.Clear();
        txtCity.Clear();
    }

private void btnPrevious_Click(object sender, RoutedEventArgs e)
    {
      // need implementation code
    }
private void btnNext_Click(object sender, RoutedEventArgs e)
    {
      // need implementation code
    }

Thanks 谢谢

Assuming you are not using any binding stuff, you should write a helper method that populates your UI with the values from your currently selected Student object: 假设您没有使用任何绑定内容,您应该编写一个帮助器方法,使用当前所选Student对象的值填充UI:

private void bindStudent(Student student) {
    if (student != null) {
        txtFirstName.Text = student.FirstName;
        txtLastName.Text = student.LastName;
        txtCity.Text = student.City;
    }
    else { // Just a fail-safe.
        txtFirstName.Clear();
        txtLastName.Clear();
        txtCity.Clear();
    }
}

Then your Next and Previous buttons only need to fetch the respective object from your studentList and call the method with it. 然后你的Next和Previous按钮只需要从studentList获取相应的对象并用它调用方法。 One thing you need to do is to remember which object in your list is currently selected in order to get the "next" and "previous" objects. 您需要做的一件事是记住当前选择列表中的哪个对象以获取“下一个”和“上一个”对象。

private int curSelectionIndex = -1;

private void btnPrevious_Click(object sender, RoutedEventArgs e)
{
  if (curSelectionIndex - 1 >= 0)
  {
      curSelectionIndex--;
      bindStudent(studentList[curSelectionIndex]);
  }
}

private void btnNext_Click(object sender, RoutedEventArgs e)
{
  if (curSelectionIndex + 1 < studentList.Count)
  {
      curSelectionIndex++;
      bindStudent(studentList[curSelectionIndex]);
  }
}

Your question didn't specify what you want to do with the UI after creating a fresh student, so I didn't do anything in regards to updating that selection. 您的问题未指定在创建新学生后您想要使用UI做什么,因此我没有做任何有关更新该选择的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM