2010-03-19 10 views
0

그래서 나는 다음과 같은 모델이 있습니다WPF 콤보 상자 바인딩

public class Person 
{ 
    public String FirstName { get; set; } 
    public String LastName { get; set; } 
    public String Address { get; set; } 
    public String EMail { get; set; } 
    public String Phone { get; set; } 
} 

public class Order 
{ 
    public Person Pers { get; set;} 
    public Product Prod { get; set; } 
    public List<Person> AllPersons { get; set; } 

    public Order(Person person, Product prod) 
    { 
    this.Pers = person; 
    this.Prod = prod; 
    AllPersons = database.Persons.GetAll(); 
    } 

} 

을 내가 주문을 편집하는 데 사용되는 WPF 창을 가지고있다. DataContext를 Order로 설정했습니다.

public SetDisplay(Order ord) 
{ 
DataContext = ord; 
} 

나는 다음과 같은 XAML 있습니다

<ComboBox Name="myComboBox" 
      SelectedItem = "{Binding Path=Pers, Mode=TwoWay}" 
      ItemsSource = "{Binding Path=AllPersons, Mode=OneWay}" 
      DisplayMemberPath = "FirstName" 
      IsEditable="False" /> 


<Label Name="lblPersonName" Content = "{Binding Path=Pers.FirstName}" /> 
<Label Name="lblPersonLastName" Content = "{Binding Path=Pers.LastName}" /> 
<Label Name="lblPersonEMail" Content = "{Binding Path=Pers.EMail}" /> 
<Label Name="lblPersonAddress" Content = "{Binding Path=Pers.Address}" /> 

는하지만, 내가 선택한 항목을 변경하면 ....... 작동하지 않는 바인딩, 레이블이 업데이트되지 않습니다를 ... ..

감사합니다 !!

답장을 보내 주시면 감사하겠습니다.

+0

있습니까? AllPersons.Contains (person)가 생성자에서 true를 반환합니까? 나는 아니 겠지! 도움이되는 게시물을 답변으로 표시하는 것을 잊어 버리지 마십시오. 그렇지 않으면 아무도 미래에 도움을 줄 수 없습니다. –

+0

예 - 사람이 AllPersons에 100 % 확신합니다. – MadSeb

답변

1

모델은 변경 알림을 발동해야합니다. INotifyPropertyChangedINotifyCollectionChanged을 참조하십시오.

INotifyPropertyChanged의 경우 기본 ViewModel 클래스 (예 : this one)를 사용할 수 있습니다. 컬렉션의 경우 ObservableCollection<T>이 최선을 다하고 있습니다. 그러나 UI가 바인딩 된 후에는 컬렉션이 변경되지 않으므로 관찰 가능한 컬렉션이 필요하지 않습니다. 그럼에도 불구하고 일반적으로 뷰 모델 레이어에서 관찰 가능한 컬렉션을 사용하여 코드가 변경되는 경우 헤드 스크래칭을 절약하는 것이 좋습니다.

이 어떻게 보이는지의 예는 다음과 같습니다 당신은 사람이 AllPersons 수집에 있는지 확인

public class Person : ViewModel 
{ 
    private string firstName; 
    private string lastName; 
    private string email; 
    private string phone; 

    public string FirstName 
    { 
     get 
     { 
      return this.firstName; 
     } 
     set 
     { 
      if (this.firstName != value) 
      { 
       this.firstName = value; 
       OnPropertyChanged(() => this.FirstName); 
      } 
     } 
    } 

    public string LastName 
    { 
     get 
     { 
      return this.lastName; 
     } 
     set 
     { 
      if (this.lastName != value) 
      { 
       this.lastName = value; 
       OnPropertyChanged(() => this.LastName); 
      } 
     } 
    } 

    // and so on for other properties 
} 

public class Order : ViewModel 
{ 
    private readonly ICollection<Person> allPersons; 
    private Person pers; 
    private Product prod; 

    public Person Pers 
    { 
     get 
     { 
      return this.pers; 
     } 
     set 
     { 
      if (this.pers != value) 
      { 
       this.pers = value; 
       OnPropertyChanged(() => this.Pers); 
      } 
     } 
    } 

    public Product Prod 
    { 
     get 
     { 
      return this.prod; 
     } 
     set 
     { 
      if (this.prod != value) 
      { 
       this.prod = value; 
       OnPropertyChanged(() => this.Prod); 
      } 
     } 
    } 

    // no need for setter 
    public ICollection<Person> AllPersons 
    { 
     get 
     { 
      return this.allPersons; 
     } 
    }  

    public Order(Person person, Product prod) 
    { 
     this.Pers = person; 
     this.Prod = prod; 

     // no need for INotifyCollectionChanged because the collection won't change after the UI is bound to it 
     this.allPersons = database.Persons.GetAll(); 
    } 
}