Combobox同步所选值

时间:2014-07-24 16:18:54

标签: c# winforms

以下代码使用值

填充我的组合框
List<Filename> fnList = new List<Filename>();
fnList.Add(new Filename("test1.png"));
fnList.Add(new Filename("test2.png"));
fnList.Add(new Filename("test3.jpg"));
comboBox1.DataSource = fnList;
comboBox1.DisplayMember = "Name";

现在,下面的代码应该将选定的值与关联对象中的成员同步。

comboBox1.DataBindings.Clear();
comboBox1.DataBindings.Add("SelectedValue", copy, "EventPicture");

copy属于某个类,其中EventPicture是字符串属性。这是我希望每次用户更改组合框选择时同步所选值,并且每次EventPicture值从代码的其他部分更改时都会同步。

此时我收到了一个错误:

Cannot set the SelectedValue in a ListControl with an empty ValueMember.

1 个答案:

答案 0 :(得分:1)

您还需要在ComboBox上设置ValueMember

comboBox1.ValueMember = "Name";
相关问题