Windows窗体中的数据绑定

时间:2016-10-08 09:22:53

标签: c# winforms

当我尝试将数据从设计器上的对象绑定到我的组合框时,不显示项目。enter image description here

但是当我从后面的代码中指定时会显示它们...... enter image description here

如何从设计师绑定我的收藏品?

2 个答案:

答案 0 :(得分:1)

如果要从设计器绑定项目 选择 - >项目集合来自属性窗口 pic

从列表中以编程方式添加

 List<string> values = new List<string>();

    private void AddItemProg()
    {
        values.Add("Name");
        values.Add("Age");
        values.Add("DOB");
        values.Add("Address");

        comboBox1.Items.Clear();

        for (int nIndex = 0; nIndex < values.Count; nIndex++)
        {
            string v = values[nIndex];
            comboBox1.Items.Add(v);
        }
    }

答案 1 :(得分:1)

您不需要使用observablecollection来绑定Windows窗体应用程序中的UI。只需设置combobox.ItemSource = List<string>即可。当你想获得当前值时,只需使用combobox.SelectedItem或combobox.SelectedValue获取当前值。
(ps.observable属性应该有get和set方法,在set方法中你需要调用方法RaisePropertyChanged("propertyname"),为此你还需要在UI部分做一些更改并导入一些。(类似的东西,我不要&# 39;记住它究竟是如何运作的,但它很复杂。