winforms icon indicating copy to clipboard operation
winforms copied to clipboard

DataBinding: incompatible data type conversion shows no reaction

Open VaporiZed opened this issue 1 year ago • 2 comments

.NET version

7.0

Did it work in .NET Framework?

No

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No response

Issue description

If a DataBinding is created between two properties of incompatible data types, nothing happens at runtime, so it's harder to find the causing problem. I bind a property of a control to a property of a class via a BindingSource. If the data types of the properties fit, everything is fine. If they doesn't fit, the setter of the property won't be reached. But there is also no exception about the problem. Neither when not-reaching the setter nor at the app start to tell be that a DataBinding problem exists.

Steps to reproduce

Test project: WinFormsNetVB.zip

  • start the app
  • click the button
  • the setters of both properties of the Foo class will be reached
  • stop the app
  • change the code: in the class Foo, change the type of the field _ClassB and the property ClassB from ClassB to ClassA CodeChange
  • start the app
  • click the button
  • the setter of the ClassB property of the Foo class will not be reached. There is also no exception message.

I expect an exception that tells me, that the data bound properties are type-incompatible, either when setting the data binding (mostly at app start) or at least when the magic of data binding will be activated after changing one of the bound properties.

VaporiZed avatar Mar 10 '24 22:03 VaporiZed

@KlausLoeffelmann might be the person to talk to about this.

elachlan avatar Mar 11 '24 10:03 elachlan

"Funny" enough, there actually is a build-in infrastrucutre in WinForms, and for its days, it is not even bad. The thing is, it's a nightmare to discover.

And that's the reason, we need to consider a safe and predictable way to expose this. I say safe, because the internals purely based on reflection might not be ideal, let alone, when it comes to performance.

So, my proposal is introducing IValueConverter for WinForms, which can get honored by the BindingClass. I think, that's a reasonable thing to do. And shouldn't be too hard.

@elachlan: BTW: It's the MVP Summit this week, so I am super busy and hard to get. But feel free to take a look what I am doing in-between, and feel also free to ping me directly, if you have ideas, concerns, questions, you might want to address quickly. Thanks for your help here!

KlausLoeffelmann avatar Mar 12 '24 19:03 KlausLoeffelmann

@KlausLoeffelmann what would be the next step for this issue then? Can you point to the existing docs for the feature?

JeremyKuhne avatar Mar 19 '24 20:03 JeremyKuhne

@KlausLoeffelmann do you have an issue for the proposal you cite? If not please create one and link it here. We'll then be able to close this issue as it will be resolved by the Converters proposal.

merriemcgaw avatar Apr 03 '24 20:04 merriemcgaw

Hi @merriemcgaw and @KlausLoeffelmann! We are thinking for various options to enable our clients use the DateOnly and TimeOnly types inside our DateTimePicker control. The control is currently working with the DateTime struct. Definitely support for the IValueConverter interface inside the BindingClass could help. Will this feature make it to .NET 9?

icom85 avatar Aug 27 '24 08:08 icom85

@icom85 this feature won't make .NET 9, but if it's something that will be beneficial I can get it on the backlog for .NET 10. I can see the benefit for sure. @KlausLoeffelmann / @JeremyKuhne what do you guys think?

merriemcgaw avatar Aug 27 '24 23:08 merriemcgaw