简体   繁体   中英

Winforms ComboBox DataBinding DisplayMember to SubObject Property

i searched for 2 hours or more and can not find an answer. So i try it here:

I want to know how (and if it can be done at all) can i databind a List of Models to a WinForms ComboBox, and use a Property of a Property of the Model (thats in the List) as DisplayMember ? See Code here:

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

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

Here is a similar question. In the marked answer, you can see that they set the BindingContext of the Form on the ComboBox and it seems to work for them...

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

Hope that helps...

Ok, I have the DisplayMember working. You cant use "Home" as a ValueMember as its an object, you will have to point at one of its properties(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;

    }
}

To clarify: ComboBox uses always the hosting forms BindingContext per default. I tested it with that code:

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

and the message box has shown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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