Data binding works differently in .NET 4.0 from 2.0

Does anyone know why data binding works in .NET 4.0 differently? That is, in .NET 4.0, RelatedPropertyManager.GetItemProperties () returns properties from the parent type of the object, and not from the corresponding one, as in the following code example:

using System; using System.Windows.Forms; static class Program { static void Main() { using (Form form = new Form()) { ParentObject parent = new ParentObject(); BindingManagerBase bindingManager = form.BindingContext[parent, "Nested"]; Console.WriteLine("Has Code1 property: " + (bindingManager.GetItemProperties()["Code1"] != null)); Console.WriteLine("Has Code2 property: " + (bindingManager.GetItemProperties()["Code2"] != null)); // .NET 2.0: // Has Code1 property: False // Has Code2 property: True // .NET 4.0: // Has Code1 property: True // Has Code2 property: False Console.ReadKey(); } } class ParentObject { public NestedObject Nested { get; set; } public string Code1 { get; set; } } class NestedObject { public string Code2 { get; set; } } } 

I also asked a question about Microsoft Connect, but there is no answer yet.

+4
source share
3 answers

The cause of the problem is in PropertyManager.GetItemProperties (PropertyDescriptor [] list accessors), which is called from RelatedPropertyManager. In .NET 4, listAccessors are ignored:

 internal override PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors) { if (this.dataSource == null) return new PropertyDescriptorCollection((PropertyDescriptor[]) null); else return TypeDescriptor.GetProperties(this.dataSource); } 

In .NET 2.0, it was like this:

 internal override PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors) { return ListBindingHelper.GetListItemProperties(this.dataSource, listAccessors); } 

But I'm not sure if this is a bug or an intentional change in functionality.

+3
source

It is not unheard of for things (even important things) to change major versions of a release. In this scenario, frameworks differ in two major versions.

In addition, you may also notice that a client with the .NET 4.0 Framework installed will not be able to run .NET 2.0 (or 3.x) applications, which, unlike a client with 3.x installed to run 2.0..NET 4.0 applications, is large jump with a lot of changes, including the changes that @Daniel mentioned.

+2
source

Such questions always raise the question: is this a mistake or a function? I can’t imagine that this is the expected behavior, because with .net 3.5 (2.0) you get 4.0 results when you do BindingManagerBase bindingManager = form.BindingContext[parent] (without "Nested").

Why BindingContext have the Item[dataSource, dataMember] property when the dataMember argument is not valid? Please let us know about the Microsoft problem.

+1
source

All Articles