【问题标题】:Combobox to sync selected value用于同步所选值的组合框
【发布时间】:2014-07-24 16:18:54
【问题描述】:

以下代码正在用值填充我的组合框

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.

【问题讨论】:

  • 您是否尝试在组合框上设置ValueMember
  • 啊,这很快解决了问题,现在同步工作了 :) 我怎么能错过...

标签: c# winforms


【解决方案1】:

您还需要在 ComboBox 上设置ValueMember

comboBox1.ValueMember = "Name";

【讨论】:

    猜你喜欢
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    相关资源
    最近更新 更多