无法绑定到DataSource上的属性或列名称.参数名称:dataMember

Evg*_*eny 8 c# asp.net data-binding

当我尝试绑定到System.Type.Name时,这是我遇到的异常.

这是我正在做的事情:

this.propertyTypeBindingSource.DataSource = typeof(System.Type);

/* snip */

this.nameTextBox1.DataBindings.Add(
    new System.Windows.Forms.Binding(
        "Text", 
        this.propertyTypeBindingSource, 
        "Name", true));
Run Code Online (Sandbox Code Playgroud)

是否有一些绑定到System.Type的技巧,是不允许的还是有任何解决方法?绑定到其他类型没有问题.

Mar*_*ell 13

实际上,对Type有特殊处理......这种方法在IDE等中用于提前配置元数据.如果你看看IDE生成的绑定,他们会做以下事情:

bindingSource1.DataSource = typeof(MyObject);
Run Code Online (Sandbox Code Playgroud)

说"当我们获得真实数据时,我们期望MyObject isntance(s)"; 即当你要求"Name"时,它在MyObject上寻找name属性- 而不是Type实例的Name.这允许网格等获取其元数据而无需等待实际数据; 但结果你不能绑定到"真实"类型.

System.ComponentModel代码在简单绑定和列表绑定(提供或接受货币管理器)之间是相同的,因此简单绑定也会继承此行为.同样,您不能绑定到实现IList/IListSource的类的属性,因为这是以特殊方式解释的.

你的额外课程似乎是一种合理的方法


Evg*_*eny 3

找到了解决方法。做了一堂课

public class StubPropertyType
{
    public StubPropertyType(Type type)
    {
        this.StubPropertyTypeName = type.Name;
    }

    public string StubPropertyTypeName = string.Empty;
}
Run Code Online (Sandbox Code Playgroud)

创建了一个绑定源

this.propertyStubBindingSource.DataSource = typeof(StubPropertyType);
Run Code Online (Sandbox Code Playgroud)

创建该类的一个实例并将文本框绑定到它。

this.nameTextBox.DataBindings.Add(
    new System.Windows.Forms.Binding(
        "Text", 
        this.propertyStubBindingSource, 
        "StubPropertyTypeName", 
        true));
Run Code Online (Sandbox Code Playgroud)

完全按照要求工作。