I have Student table, Course table and a StudentCourse table. On my WPF, I’m making “Student” selections with combo box. How can I display the corresponding Course and its properties(CourseID, CourseName, Credit) in a listview, using ObservableCollection?. Here’s my code for the combo box SelectionChanged event
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int MySelect = (int)this.comboBox1.SelectedValue;
var SelectedStudent = db.Students.Include("StudentCourses.Course").SingleOrDefault(f => f.StudentID == MySelect);
If you’re not already, I would highly recommend using a MVVM styled approach. Your ListView’s ItemsSource should be bound to an ObservableCollection in your ViewModel and your ComboBox’s SelectedItem should also be bound to a propety on your ViewModel. When the SelectedItem changes and calls the property’s setter update the ObservableCollection that your ListView is bound to.
Update:
Here’s a partially implemented solution:
XAML:
Code: