작동하지 않는 목록의 인스턴스에 사용자 지정 목록과의 selectedItem에 itemsource 결합하지만, 난 여전히 여기 :(콤보 내가 일하는 내 콤보를 만들기 위해 정말 다른 방법을 시도
붙어 매우 단순화 된 버전입니다 해요 내 응용 프로그램 (단지 실수 죄송합니다, 편집)
<ListView ItemsSource="{Binding People}" SelectedItem="{Binding SelectedPerson}"/>
<ComboBox ItemsSource="{Binding Grades}" SelectedItem="{Binding SelectedPerson.MyGrade}"
DisplayMemberPath="Name"/>
그리고 뒤에있는 코드 :
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
}
문제는 내가 항목에서를 선택하면 콤보가 여전히 비어 있다는 것입니다 listview. itemsource는 괜찮습니다 (콤보 상자를 클릭하면 "1 학년"과 "2 학년"을 볼 수 있습니다). "Person.Grade
은 Grades
목록에 포함되어 있습니다."라고 말하면서 누락 된 부분이 있습니다.하지만 찾을 수 없습니다. ,
이 (가)'은'ItemsSource'의 항목으로 메모리에 동일한 기준을 SelectedItem'인가? 기본적으로 WPF는 항목을 참조로 비교하므로 문제가 될 수 있습니다. 그것들이 다른 경우,'SelectedValue'를 사용하거나'.Equals()'를 덮어 쓰는 것이 좋습니다. 예를 들어 [here] (http://rachel53461.wordpress.com/2011/08/20/comboboxs-selecteditem-not-displaying/) 참조하십시오. – Rachel
문제는 그런 것에서 비롯된 것 같습니다. 그렇지 않습니다. selecteditem과 itemssource가 같은 참조에서 온다고 생각하십시오. 나는 오늘 밤 그걸 시험 할거야;) – anthoLB29
정말 고마워 !! equals 오버라이드 메소드는 저를 구해줬습니다 :) :) – anthoLB29