dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.43k stars 986 forks source link

DataBinding: incompatible data type conversion shows no reaction #11020

Open VaporiZed opened 8 months ago

VaporiZed commented 8 months ago

.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

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.

elachlan commented 8 months ago

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

KlausLoeffelmann commented 8 months ago

"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!

JeremyKuhne commented 8 months ago

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

merriemcgaw commented 7 months ago

@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.

icom85 commented 2 months ago

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?

merriemcgaw commented 2 months ago

@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?