BindingSource的DataDource可以是表单上的属性和对象吗?

时间:2009-05-09 11:49:52

标签: c# bindingsource bindinglist

例如: 我在表单上有一个Contact对象(见下文)。我可以将BindingSource的数据源属性设置为Contact.Addresses属性。

类AddressCollection实现了BindingList,因此在未被Contact类封装时不存在绑定问题。

public class Contact : IComparable<Contact>, IComponent
{
    #region Properties
    private AddressCollection addresses = new AddressCollection();
    private ContactNumberCollection contactNumbers = new ContactNumberCollection();

    public int ContactId { get; set; }
    public string Title { get; set; }
    public string Forename { get; set; }
    public string MiddleName { get; set; }
    public string Surname { get; set; }
    public string JobTitle { get; set; }
    public DateTime DateAdded { get; set; }
    public DateTime LastUpdate { get; set; }

    public AddressCollection Addresses
    {
        get { return addresses; }
        set { addresses = value; }
    }
    public ContactNumberCollection ContactNumbers
    {
        get { return contactNumbers; }
        set { contactNumbers = value; }
    }
    #endregion

    #region Constructors
    public Contact()
    {
        DateAdded = DateTime.Now;
        LastUpdate = DateTime.Now;
    }

    #endregion

    public int CompareTo(Contact other)
    {
        return FullName.CompareTo(other.FullName);
    }

    #region IComponent Objects
    private ISite mSite;

    public event EventHandler Disposed;

    public virtual void Dispose()
    {
        if ((mSite != null) && (mSite.Container != null))
        {
            mSite.Container.Remove(this);
        }

        Disposed(this, System.EventArgs.Empty);
    }

    public ISite Site
    {
        get
        {
            return mSite;
        }
        set
        {
            mSite = value;
        }
    }
    #endregion
}

谢谢Anthony

1 个答案:

答案 0 :(得分:0)

简而言之,是的,你可以。但是,如果您需要更改联系人,可能需要将DataSource设置为联系人(或联系人列表),并使用DataMember“地址”。

这是一个显示此类绑定的简化示例。如果我错过了你的观点,请澄清:

using System;
using System.ComponentModel;
using System.Windows.Forms;
class AddressCollection : BindingList<Address>
{
    public void Add(string line1, string zip)
    { // just for convenience
        Add(new Address { Line1 = line1, Zip = zip});
    }
}

class Address
{
    public string Line1 { get; set; }
    public string Zip { get; set; }

    public override string ToString()
    {
        return Line1;
    }
}
class Contact
{
    public string Name { get; set; }
    private readonly AddressCollection addresses = new AddressCollection();
    public AddressCollection Addresses { get { return addresses; } }
    public override string ToString()
    {
        return Name;
    }
}

static class Program
{
    [STAThread]
    static void Main()
    {
        BindingList<Contact> contacts = new BindingList<Contact>
        {
            new Contact { Name = "Fred", Addresses = { {"123 Some road", "AB1"}}},
            new Contact { Name = "Barney", Addresses = { {"456 A Street", "CD2"}, {"789 The Lane", "EF3"}}}                
        };
        BindingSource bs = new BindingSource(contacts, "");

        Form form = new Form { 
            Controls = {
                                new DataGridView { Dock = DockStyle.Fill, DataSource = bs, DataMember = "Addresses" },
                new ComboBox { Dock = DockStyle.Top, DataSource = bs, DisplayMember = "Name" },
                new TextBox { Dock = DockStyle.Bottom, DataBindings = { {"Text", bs, "Addresses.Zip"}}}
            }
        };
        Application.Run(form);

    }
}