I tried really different ways to do my list work, but I was still stuck :(
Here is a very simplified version of my application: (just edited, sorry for the errors)
<ListView ItemsSource="{Binding People}" SelectedItem="{Binding SelectedPerson}"/> <ComboBox ItemsSource="{Binding Grades}" SelectedItem="{Binding SelectedPerson.MyGrade}" DisplayMemberPath="Name"/>
And the code behind:
public class Person { private string name; public string Name { get { return name; } set { if (name != value) { name = value; NotifyPropertyChanged("Name"); } } } private Grade myGrade; public Grade MyGrade { get { return myGrade; } set { if (myGrade != value) { myGrade = value; NotifyPropertyChanged("MyGrade"); } } } //-- INotifyPropertyChanged implementation } public class Grade { private string name; public string Name { get { return name; } set { if (name != value) { name = value; NotifyPropertyChanged("Name"); } } } private int prop; public int Prop { get { return prop; } set { if (prop != value) { prop = value; NotifyPropertyChanged("Prop"); } } } //-- INotifyPropertyChanged implementation } public partial class MainWindow : Window { public ObservableCollection<Person> People { get; set; } public ObservableCollection<Grade> Grades { get; set; } private Person selectedPerson; public Person SelectedPerson { get { return selectedPerson; } set { if (selectedPerson != value) { selectedPerson = value; NotifyPropertyChanged("SelectedPerson"); } } } public MainWindow() { InitializeComponent(); this.DataContext = this; People = new ObservableCollection<Person>(); Grades = new ObservableCollection<Grade>(); Grades.Add(new Grade() { Name = "Grade 1", Prop = 1 }); Grades.Add(new Grade() { Name = "Grade 2", Prop = 2 }); People.Add(new Person() { Name = "guy 1", MyGrade = Grades[0] }); People.Add(new Person() { Name = "guy 2", MyGrade = Grades[0] }); People.Add(new Person() { Name = "guy 3", MyGrade = Grades[1] }); } //-- INotifyPropertyChanged implementation }
The problem is that combobox is still empty when I select an item from the list. The data source is in order (if I click on the combo box, I see "class 1" and "class 2"). I think there is something implicit to say that Person.Grade is part of the Grades list, but I cannot find that.
I hope you help me;)
c # data-binding wpf combobox selecteditem
anthoLB29
source share