2014-09-11 204 views
0

我有一個WPF數據網格,我想綁定到以下數據模型,但我似乎無法得到它正確。最初,我綁定的列表將是空的,我想讓用戶能夠輸入信息。所以我定義了數據網格,例如:問題綁定到WPF Datagrid

<DataGrid Name="dgUsers" AutoGenerateColumns="True" CanUserAddRows="True" CanUserDeleteRows="True"/> 

我的模型類如下:

public class User 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public DateTime Birthday { get; set; } 

    public string Details 
    { 
     get 
     { 
      return String.Format("{0} was born on {1} and this is a long description of the person.", this.Name, this.Birthday.ToLongDateString()); 
     } 
    } 
} 

public class Group : INotifyPropertyChanged 
{ 
    private string _id = Guid.NewGuid().ToString(); 

    private string _name; 
    public string Name 
    { 
     get { return this._name; } 
     set 
     { 
      if (value != this._name) 
      { 
       this.Name = value; 
       OnPropertyChanged("Name"); 
      } 
     } 
    } 

    public string ID { get { return this._id; } } 
    public List<User> GroupUsers 
    { 
     get; 
     set; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

我希望做的是對集團類綁定,但只對GroupUsers值。我怎麼做?我有我的用戶屬性GroupUsers正確定義爲TwoWay綁定?

回答

3

你需要有User類實現INotifyPropertyChangedGroupUsers屬性應該是一個ObservableCollection<T>

+0

好的,謝謝。那麼,我要如Name屬性一樣遵循INotifyPropertyChange模式嗎? – 2014-09-11 10:42:19

+0

是的。對於User類中的每個公共屬性。 – toadflakz 2014-09-11 10:45:03

+0

謝謝你,它現在似乎是正確的綁定!感謝您的幫助。 – 2014-09-11 11:25:16