2010-03-19 215 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(); 
    } 
}