开发者

WPF ComboBox Binding

So I have the following model:

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

}

And I have a WPF window used to edit an order. I set the DataContext to Order.

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

I have the following XAML:

<ComboBox Name="myComboBox"
          SelectedItem = "{Binding Path=Pers, Mode=TwoWay}"
          ItemsSource = "{Binding Path=AllPersons, Mode=OneWay}"
          DisplayMemberPath = "FirstName"
          IsEditable="F开发者_运维知识库alse" />


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

However, the binding does not seem to work.......When I change the selected item , the labels do not update ....

Regards!!

Any reply is appreciated !!


Your model will need to fire change notifications. See INotifyPropertyChanged and INotifyCollectionChanged.

For INotifyPropertyChanged, you could use a base ViewModel class such as this one. For collections, ObservableCollection<T> does the hard work for you. However, in your case your collection won't change after the UI is bound to it, so you shouldn't need an observable collection. Regardless, I'd generally recommend using observable collections in your view model layer to save head-scratching should the code ever change.

An example of what this would look like is:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜