Combobox binding itemsource to custom list and selecteditem to an instance of that list doesn't work

前端 未结 3 1832
轮回少年
轮回少年 2021-01-13 20:19

I tried really different ways to make my combobox working but I\'m still stuck :(

Here is a very simplified version of my app : (just edited, sorry for mistakes)

3条回答
  •  天涯浪人
    2021-01-13 20:40

    A couple things, first you are binding to Grade and your property is called MyGrade. Second you are instantiating different objects for grade in the Grades list and using different objects to assign to each person, you want to use the same objects if you want them to map correctly, something like this:

    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] });
    

    Lastly you might want to make the Grades collection an ObservableCollection as well if you are going to bind it to the UI. Also it seems a bit odd that your RaisePropertyChanged doesn't take in the name of the property.

    There may be other mistakes as well but that is what jumped out to me right away.

提交回复
热议问题