Jake McBride
Jake McBride

Reputation: 55

Set label value to that of a property of a given object that is bound to a listbox

So I have a ListBox that is bound to a list of student objects through...

listBox_studentListBox.ItemsSource = studentList;

I have a button that will save data to the list.

private void btn_studentListBox_Add_Click(object sender, RoutedEventArgs e)
{
      currentID = getStudentID();//getStudentID() just finds an available student ID, used to recycle ids of deleted student
      studentList.Add(new cls_student() { studentName = txtBox_studentInfoName.Text, studentID = currentID });
      listBox_studentListBox.DisplayMemberPath = "studentName";
      listBox_studentListBox.SelectedValue = "studentID";
      listBox_studentListBox.Items.Refresh();
}

I now when I click on a given item in the ListBox I want to load it's data. I am trying to do that here.

private void listBox_studentListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
     //Load data into labels
}

My student object is here.

public class cls_student
{
    public string studentName { get; set; }
    public int studentID { get; set; }

    public List<cls_assignment> assignmentList = new List<cls_assignment>();
}

As you can see I have another list stored inside the student object, but we will worry about loading that list into a different ListBox later.

My concern is how do I access a specific student object based on my selection (just physically left clicking an object in the ListBox calling the SelectionChanged event handler) to be able to display that data in labels for the user.

I understand that by setting the DisplayMemberPath in my Add event handler I can choose what the ListBox displays each binded student object as. The issue is that I am not how to bind some sort of value to that object in the ListBox. My idea is if I can bind a value to that object then I can be able to access the exact student object I want when I select it to then load it's data into labels.

Any and all feedback is appreciated - this is a updated post of recent post that was asked to be taken down due to my adding objects to a ListBox's .Items property rather than binding it with ItemsSource.

EDIT: Thanks to a reply I see that if I do SelectedItem I can get to the object. I now have this code.

private void listBox_studentListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
      testlabel.Content = "SelectedIndex: " + listBox_studentListBox.SelectedIndex;
      cls_student selectedStudent = (cls_student)listBox_studentListBox.SelectedItem;
      lbl_studentInfoName.Content = selectedStudent.studentName;
}

My question is if selectedStudent's scope is only during the event handler or if it lasts past it? For context I am used to C++ where I have to delete EVERYTHING or else I get memory leaks. Newer to C# I am.

Upvotes: 0

Views: 210

Answers (2)

Salah Akbari
Salah Akbari

Reputation: 39966

You should use Binding:

<ListBox Name="listBox_studentListBox">
     <ListBox.ItemTemplate>
         <DataTemplate>
             <WrapPanel>
                 <TextBlock Text="{Binding studentName}" />
             </WrapPanel>
         </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>

And for Label:

<Label Content="{Binding ElementName=listBox_studentListBox, Path=SelectedItem.studentID}" />

And in the code behind:

currentID = getStudentID();
studentList.Add(new cls_student() { studentName = txtBox_studentInfoName.Text, studentID = currentID });
listBox_studentListBox.ItemsSource = studentList;

Upvotes: 1

user3230660
user3230660

Reputation:

Put a Student property on your view model. Raise PropertyChanged when it changes.

Bind the SelectedItem of your listbox to Student property.

Upvotes: 0

Related Questions