简体   繁体   English

无法绑定到WPF Datagrid

[英]Trouble Binding to WPF Datagrid

I have a WPF datagrid that I wish to bind to the following data model, but I can't seem to get it correct. 我有一个WPF数据网格,我希望绑定到以下数据模型,但是我似乎无法正确理解它。 Initially, the List that I am binding to will be empty, and I want to give the user the ability to enter in the information. 最初,我要绑定的列表将为空,并且我想赋予用户输入信息的能力。 So I define the data grid as such: 所以我这样定义数据网格:

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

My model class is as follows: 我的模型类如下:

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

What I would want to do is bind on the Group class, but only to the GroupUsers value. 我想做的是在Group类上绑定,但仅绑定到GroupUsers值。 How do I do that? 我怎么做? Do I have my User's property GroupUsers defined correctly for TwoWay binding? 我是否为TwoWay绑定正确定义了用户的属性GroupUsers?

您需要具有User类实现INotifyPropertyChanged并且GroupUsers属性应该是ObservableCollection<T>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM