【问题标题】:Binding to a object which also implements IEnumerable<>绑定到也实现 IEnumerable<> 的对象
【发布时间】:2018-07-09 12:38:13
【问题描述】:

我有以下课程:

public class FunctionList : IEnumerable<Function> {
   public string Comments {get; set;}

    public IEnumerator<Function> GetEnumerator() {
        return ...;
    }

    IEnumerator IEnumerable.GetEnumerator() {
        return this.GetEnumerator();
    }
}

这个类的一个对象被用作WinForm中Binding的数据源:

this.checkEdit1.DataBindings.Add(new Binding(nameof(this.checkEdit1.EditValue), functionList, nameof(functionList.Comments), false, DataSourceUpdateMode.OnPropertyChanged));

问题是,当dataSource (functionList) 实现IEnumerable&lt;&gt; 时,绑定不起作用。当我删除此接口时,它工作正常。否则我会得到以下ArgumentException:

Cannot bind to the property or column Comments on the DataSource.
Parameter name: dataMember

当dataSource 实现IEnumerable&lt;&gt; 时,我该怎么做才能使绑定工作?

【问题讨论】:

  • 自定义 IEnumerable 背后的任何原因,而不是使用许多内置类型之一,如 List&lt;T&gt;?
  • 我需要在其他地方使用 IEnumerable-Interface 的对象。但在这种绑定场景下,它应该被忽略。

标签: c# winforms binding


【解决方案1】:

IEnumerable

Windows 窗体控件可以绑定到仅支持 IEnumerable 接口的数据源,前提是它们通过 BindingSource 组件。

https://docs.microsoft.com/en-us/dotnet/framework/winforms/data-sources-supported-by-windows-forms

【讨论】:

  • 目标是,Binding-Mechanism 忽略 IEnumerable-Interface。
  • @BennoDual 这就是文档所说的将会发生的事情。您需要使用 BindingSource 才能使其工作。
  • 文档说当我想绑定到 IEnumerable 时,我需要一个 BindingSource。但不想绑定到 IEnumerable-Interface。我只想绑定到一个简单的属性。
猜你喜欢
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
  • 2014-10-29
  • 2014-11-08
  • 2013-06-03
  • 2017-05-22
  • 2017-04-14
相关资源
最近更新 更多