c# – Winforms ComboBox DataBinding DisplayMember到SubObject属性

前端之家收集整理的这篇文章主要介绍了c# – Winforms ComboBox DataBinding DisplayMember到SubObject属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
搜索了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; }
}

解决方法@H_403_8@
刚刚添加到你的代码一个方法(事实上),它的工作.
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,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 – >属性.

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

在下面的事件列表中查找格式(在“更改的属性”下),并键入一些事件名称,如下所示:ComboBoxFormat,然后按Enter键.而已 原文链接:https://www.f2er.com/csharp/95047.html

猜你在找的C#相关文章