麻烦绑定到WPF Datagrid

时间:2014-09-11 10:20:29

标签: c# wpf datagrid

我有一个WPF数据网格,我希望绑定到以下数据模型,但我似乎无法正确。最初,我绑定的List将为空,我想让用户输入信息。所以我将数据网格定义为:

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

我想要做的是绑定Group类,但仅限于GroupUsers值。我怎么做?我是否为TwoWay绑定正确定义了User的属性GroupUsers?

1 个答案:

答案 0 :(得分:3)

您需要User类工具INotifyPropertyChangedGroupUsers属性应为ObservableCollection<T>