8

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

这是我正在做的事情:

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

/* snip */

this.nameTextBox1.DataBindings.Add(
    new System.Windows.Forms.Binding(
        "Text", 
        this.propertyTypeBindingSource, 
        "Name", true));

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

4

3 回答 3

13

确实,对 Type 有特殊处理……这种方法用于 IDE 等中以提前配置元数据。如果您查看 IDE 生成的绑定,它们会执行以下操作:

bindingSource1.DataSource = typeof(MyObject);

saying "when we get real data, we expect MyObject isntance(s)"; i.e. when you ask for "Name", it is looking for the name property on MyObject - not the Name of the Type instance. This allows grids etc to obtain their metadata without having to wait for the real data; but as a consequence you can't bind to Type "for real".

The System.ComponentModel code is identical between simple bindings and list bindings (give or take a currency manager), so simple bindings also inherit this behaviour. Equally, you can't bind to properties of a class that implements IList/IListSource, since this is interpreted in a special way.

Your extra class seems a reasonable approach.

于 2008-10-21T05:55:32.597 回答
3

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

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

    public string StubPropertyTypeName = string.Empty;
}

创建了绑定源

this.propertyStubBindingSource.DataSource = typeof(StubPropertyType);

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

this.nameTextBox.DataBindings.Add(
    new System.Windows.Forms.Binding(
        "Text", 
        this.propertyStubBindingSource, 
        "StubPropertyTypeName", 
        true));

完全按照要求工作。

于 2008-10-21T00:42:55.147 回答
0

One of the possible reason for this error is table/ Dataset do not have specified column. Specially, in case of Typed DataSet make sure you have proper names in XSD matching with column names from table

于 2011-01-12T04:33:43.170 回答