Wpf 组合框绑定

Wpf 组合框绑定,wpf,combobox,flash,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 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(); 
   }

}
我有以下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}" />

但是,绑定似乎不起作用……当我更改所选项目时,标签不会更新

问候


感谢您的回复

您的模型需要触发更改通知。见和

对于INotifyPropertyChanged,可以使用基本的
ViewModel
类,例如。对于收藏品,为你做艰苦的工作。但是,在您的情况下,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(); 
    }
}
公共类人员:ViewModel
{
私有字符串名;
私有字符串lastName;
私人字符串电子邮件;
私人电话;
公共字符串名
{
得到
{
返回这个.firstName;
}
设置
{
if(this.firstName!=值)
{
this.firstName=值;
OnPropertyChanged(()=>this.FirstName);
}
}
}
公共字符串姓氏
{
得到
{
返回this.lastName;
}
设置
{
if(this.lastName!=值)
{
this.lastName=值;
OnPropertyChanged(()=>this.LastName);
}
}
}
//等其他属性
}
公共类秩序:ViewModel
{
私人只读i集合所有人;
私人私家车;
私人产品生产;
公众人物
{
得到
{
把这个还给我;
}
设置
{
if(this.pers!=值)
{
这是一个数值;
OnPropertyChanged(()=>this.Pers);
}
}
}
公共产品产品
{
得到
{
返回此.prod;
}
设置
{
if(this.prod!=值)
{
this.prod=值;
OnPropertyChanged(()=>this.Prod);
}
}
}
//不需要塞特
公众i集合所有人
{
得到
{
把这个还给所有人;
}
}    
公共秩序(个人、产品生产)
{
这个.Pers=人;
this.Prod=Prod;
//无需更改INotifyCollectionChanged,因为该集合在UI绑定到它之后不会更改
this.allPersons=database.Persons.GetAll();
}
}

您确定此人在AllPersons集合中吗?构造函数中的AllPersons.Contains(person)是否返回true?我想没有!别忘了把有用的帖子标记为答案,否则没有人会在将来帮助你。P-Person 100%肯定会进入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(); 
    }
}