WPF ComboBox绑定

时间:2010-03-19 13:54:33

标签: wpf combobox flash

所以我有以下模型:

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}" />

然而,绑定似乎不起作用.......当我更改所选项目时,标签不会更新....

此致!!

任何回复都表示赞赏!!

1 个答案:

答案 0 :(得分: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(); 
    }
}