10

我搜索了 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

4 回答 4

7

Just added to your code one method (event actually), and it's working.

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

To create this method (event), go to your form in a [Design] mode, right click on the ComboBox -> Properties.

In the top of the Properties window, click on Events (lightning icon),

look for Format in the events list below (under Property Changed) and type there some event name, let's say: ComboBoxFormat , and press Enter. That's it ;)

于 2013-09-15T22:01:14.990 回答
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;

    }
}
于 2012-12-05T14:57:52.260 回答
0

是一个类似的问题。在标记的答案中,您可以看到他们将 the 设置BindingContextFormon theComboBox并且似乎对他们有用...

//...
comboBox1.BindingContext = this.BindingContext;

希望有帮助...

于 2012-12-05T14:37:48.057 回答
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");
            }
        }

并显示了消息框。

于 2012-12-05T16:54:10.283 回答