Winforms ComboBox DataBinding DisplayMember到SubObject属性

时间:2012-12-05 13:39:58

标签: c# data-binding properties combobox

我搜索了2个小时或更长时间,找不到答案。所以我在这里试试:

我想知道如何(以及是否可以完成)我可以将模型列表数据绑定到WinForms ComboBox,并使用模型属性的属性(列表中的属性)作为DisplayMember吗?请参见此处的代码:

public partial class Form1 : Form
{
    private List<UserDataModel> userData = new List<UserDataModel>();

    public Form1()
    {
        InitializeComponent();

        MyInit();
    }

    public void MyInit()
    {
        var userDataModel1 = new UserDataModel();
        userDataModel1.Name = "Mike";
        userDataModel1.Phone = "555-666";
        userDataModel1.Home = new HomeDataModel();
        userDataModel1.Home.StreetName = "MikeStreet";
        userDataModel1.Home.GeoLocationX = 111;
        userDataModel1.Home.GeoLocationY = 222;

        var userDataModel2 = new UserDataModel();
        userDataModel2.Name = "Jonathan";
        userDataModel2.Phone = "777-888";
        userDataModel2.Home = new HomeDataModel();
        userDataModel2.Home.StreetName = "JonathanStreet";
        userDataModel2.Home.GeoLocationX = 333;
        userDataModel2.Home.GeoLocationY = 444;

        userData.Add(userDataModel1);
        userData.Add(userDataModel2);

        // This works as usually:
        /*
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "Home";
        comboBox1.DataSource = userData;
        */

        // But this works not (either with comboBox1.DataBindings.Add() nor with BindingSource):
        comboBox1.DisplayMember = "Home.StreetName";
        comboBox1.ValueMember = "Home";
        comboBox1.DataSource = userData;

        // To drive me crazy, THAT shit works:
        textBox1.DataBindings.Add("Text", userData, "Home.StreetName");

        /*
        So how can i use a String-Property of a SubObject as ComboBox-DisplayMember ???

        BTW: To rebuild the sample, you only need a normal Forms Application and
        then drop a ComboBox and a TextBox on it. Copy that code here, and run it.
        */
    }
}

internal sealed class UserDataModel
{
    public string Name { get; set; }
    public string Phone { get; set; }
    public HomeDataModel Home { get; set; }
}

internal sealed class HomeDataModel
{
    public string StreetName { get; set; }
    public int GeoLocationX { get; set; }
    public int GeoLocationY { get; set; }
}

4 个答案:

答案 0 :(得分:6)

只需在代码中添加一个方法(实际上是事件),它就可以了。

public partial class Form1 : Form
{
    private List<UserDataModel> userData = new List<UserDataModel>();
    public Form1()
    {
        InitializeComponent();

        MyInit();
    }

    public void MyInit()
    {
        var userDataModel1 = new UserDataModel();
        userDataModel1.Name = "Mike";
        userDataModel1.Phone = "555-666";
        userDataModel1.Home = new HomeDataModel();
        userDataModel1.Home.StreetName = "MikeStreet";
        userDataModel1.Home.GeoLocationX = 111;
        userDataModel1.Home.GeoLocationY = 222;

        var userDataModel2 = new UserDataModel();
        userDataModel2.Name = "Jonathan";
        userDataModel2.Phone = "777-888";
        userDataModel2.Home = new HomeDataModel();
        userDataModel2.Home.StreetName = "JonathanStreet";
        userDataModel2.Home.GeoLocationX = 333;
        userDataModel2.Home.GeoLocationY = 444;

        userData.Add(userDataModel1);
        userData.Add(userDataModel2);

        // This works as usually:
        /*
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "Home";
        comboBox1.DataSource = userData;
        */

        // But this works not (either with comboBox1.DataBindings.Add() nor with BindingSource):
        comboBox1.DisplayMember = "Home.StreetName";
        comboBox1.ValueMember = "Home";
        comboBox1.DataSource = userData;

        // To drive me crazy, THAT shit works:
        textBox1.DataBindings.Add("Text", userData, "Home.StreetName");

        /*
        So how can i use a String-Property of a SubObject as ComboBox-DisplayMember ???

        BTW: To rebuild the sample, you only need a normal Forms Application and
        then drop a ComboBox and a TextBox on it. Copy that code here, and run it.
        */
    }

    // To add this method - follow to my instructions below
    private void ComboBoxFormat(object sender, ListControlConvertEventArgs e)
    {
        e.Value = ((UserDataModel)e.ListItem).Home.StreetName;
    }
}

internal sealed class UserDataModel
{
    public string Name { get; set; }
    public string Phone { get; set; }
    public HomeDataModel Home { get; set; }
}

internal sealed class HomeDataModel
{
    public string StreetName { get; set; }
    public int GeoLocationX { get; set; }
    public int GeoLocationY { get; set; }
}

要创建此方法(事件),请在[设计]模式下转到表单,右键单击ComboBox - &gt;属性。

在“属性”窗口的顶部,单击“事件(闪电图标)”,

在下面的事件列表中查找格式(在Property Changed下)并输入一些事件名称,例如:ComboBoxFormat,然后按Enter键。 就是这样;)

答案 1 :(得分:0)

Here是一个类似的问题。在标记的答案中,您可以看到他们在BindingContext上设置了Form的{​​{1}},它似乎对他们有效......

ComboBox

希望有帮助...

答案 2 :(得分:0)

好的,我有DisplayMember工作。您不能将“Home”用作ValueMember作为其对象,您必须指向其中一个属性(StreetName,GeoLocationX,GeoLocationY)。

public partial class Form1 : Form
    {

        private List<UserDataModel> userData = new List<UserDataModel>();

        public Form1()
        {
            InitializeComponent();
            MyInit();
        }

        public void MyInit()
    {
        var mymodel = new UserDataModel("Derek", "999-999", new HomeDataModel("Sesame Street", 111, 222));

        var mymodel1 = new UserDataModel("John", "999-999", new HomeDataModel("Tin Can Alley", 333, 444));

        userData.Add(mymodel);
        userData.Add(mymodel1);

        BindingSource bs = new BindingSource();
        bs.DataSource = userData;

        comboBox1.DataSource = bs;
        comboBox1.DisplayMember = "Home.StreetName";

    }
}

internal sealed class UserDataModel
{
    public string Name { get; set; }
    public string Phone { get; set; }
    public HomeDataModel Home { get; set; }

    public UserDataModel()
    {

    }

    public UserDataModel(string name, string phone, HomeDataModel home)
    {
        this.Name = name;
        this.Phone = phone;
        this.Home = home;
    }
}

internal sealed class HomeDataModel
{
    public string StreetName { get; set; }
    public int GeoLocationX { get; set; }
    public int GeoLocationY { get; set; }

    public HomeDataModel()
    {

    }

    public HomeDataModel(string streetname, int x, int y)
    {
        this.StreetName = streetname;
        this.GeoLocationX = x;
        this.GeoLocationY = y;

    }
}

答案 3 :(得分:0)

澄清一下:ComboBox默认使用托管表单BindingContext。我用那段代码测试了它:

            var bc = comboBox1.BindingContext;
        if (bc == this.BindingContext)
        {
            if (bc.Equals(this.BindingContext))
            {
                MessageBox.Show("combobox always use same binding context as his hosting form");
            }
        }

并显示消息框。