views:

6035

answers:

2

This is the exception that I'm getting when I'm trying to bind to a System.Type.Name.

Here is what I'm doing:

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

/* snip */

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

Is there some trick with binding to System.Type, is it not allowed or is there any workaround? Have no problems with binding to other types.

+1  A: 

Found a workaround. Made a class

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

    public string StubPropertyTypeName = string.Empty;
}

created a binding source

this.propertyStubBindingSource.DataSource = typeof(StubPropertyType);

created an instance of the class and bound the textbox to it.

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

works exactly as required.

Evgeny
+4  A: 

Indeed, there is special treatment of Type... this approach is used in the IDE etc to configure meta-data ahead of time. If you look at IDE-generated bindings, they do things like:

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.

Marc Gravell