2010-03-19 262 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}" /> 

然而,結合似乎不工作.......當我改變所選擇的項目,標籤不更新.. ..

Regards !!

任何回覆讚賞!

+0

你肯定那人卻在AllPersons收藏? AllPersons.Contains(person)在構造函數中返回true嗎?我覺得不是!不要忘記標記有用的帖子作爲答案,否則沒有人會在未來幫助你 – 2010-03-19 14:00:19

+0

是的 - 人是100%肯定會在AllPersons colleciton – MadSeb 2010-03-19 15:29:42

回答

1

您的模型將需要觸發更改通知。請參閱INotifyPropertyChangedINotifyCollectionChanged

對於INotifyPropertyChanged,可以使用基類ViewModel類,如this one。對於收藏品,ObservableCollection<T>爲您做了艱苦的工作。但是,在你的情況下,你的集合在UI綁定後不會改變,所以你不應該需要一個可觀察的集合。無論如何,我通常會建議在視圖模型圖層中使用可觀察集合,以便在代碼改變時保存頭部劃痕。

的這是什麼看起來像一個例子是:

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