dotnet / docs-desktop

This repository contains .NET documentation related to Windows Forms (winforms) and Windows Presentation Foundation (WPF).
Creative Commons Attribution 4.0 International
196 stars 178 forks source link

Object requirements #1625

Closed Rose-Kenner-Biamp closed 1 year ago

Rose-Kenner-Biamp commented 1 year ago

[Enter feedback here]

This does not work at all. I suspect that 'Object' has requirements, such as implementing INotifyProperty; but none of the help sites mention this. I have a simple Object, basically a structure with 5 Properties, and the bound form components are not notified when the data changes and no BindingSource events fire.

Can you expand on what the Object is required to implement?


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

adegeo commented 1 year ago

Hi @Rose-Kenner-Biamp I'm assuming you're using Windows Forms for .NET 6 or 7? This was written for .NET Framework, and the data binding docs haven't been written for .NET yet. AFAIK there aren't any non-working parts of databinding in .NET winforms. There is a blog post that discusses some of the new .NET 6 features, but .NET 7 goes even further if you enable previewfeatures, which adds WPF-style command binding and base classes too.

Personally, I've never been able to get databinding working well in Windows Forms. Perhaps @KlausLoeffelmann can help? I believe you need to implement INotifyPropertyChanged as the easiest way to craft the object to support databinding. This article for .NET Framework explains that and I think it's still valid for .NET: https://learn.microsoft.com/en-us/dotnet/desktop/winforms/interfaces-related-to-data-binding?view=netframeworkdesktop-4.8

adegeo commented 1 year ago

Ahhhhh I've been playing with this for about 2 hours trying to figure out how it works... I think I figured it out 😂 So I did all the bindings in the designer, but then in the form load I needed to setup the DataSource of the binding source to the actual object instance. I think in the new .NET designer, the DataSource of the binding source is set to the object type, which enables the UI designer to figure out the properties and other things. But no actual object instance is set. So override the OnLoad method and set the instance:

namespace WinFormsApp1;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        bindingSource1.DataSource = new MyObjectType();
    }
}

Let me know if this gets you working.

The databinding docs for .NET Framework are just vast and badly organized. This is something we're planning on addressing when we start on porting those docs over to .NET. Sorry 😢

Rose-Kenner-Biamp commented 1 year ago

Oops. I arrived at github from DevExpress and thought I still had context! I am using Winforms with Framework 4.8.

For Datasource assignment in Load, yes: I eventually found that in a book ("Data Binding with Windows Forms 2.0" Brian Noyes) I have but could not find it online. Now your answer is online :)

That helped with my grid but not with the text controls.

On page 314 of the same book I finally found "However, if the property is changed directly through its property setter through a reference to the object, there is no way for the collection to know about the change unless the object itself notifies the collection. The support for that comes from the INotifyPropertyChanged interface."

My book does not elaborate further, though.

So my hunch that I need NotifyPropertyChanged seems to be correct; but I do not yet know exactly how to use it with Form data binding.

Rose-Kenner-Biamp commented 1 year ago

I can do it by hand.

Following any common help like https://learn.microsoft.com/en-us/dotnet/desktop/winforms/how-to-implement-the-inotifypropertychanged-interface?view=netframeworkdesktop-4.8

The object must

Ok, now it is bound.

But this is a lot of boilerplate code to write by hand - I thought there was a more convenient way to do databinding from an object.

adegeo commented 1 year ago

I'm going to close this because I don't think there is anything to do on the old doc

Rose-Kenner-Biamp commented 1 year ago

I suppose "Implement NotifyPropery" covers what must be done.

From: Andy (Steve) De George @.> Sent: Thursday, June 15, 2023 1:13 PM To: dotnet/docs-desktop @.> Cc: Rose Kenner @.>; Mention @.> Subject: Re: [dotnet/docs-desktop] Object requirements (Issue #1625)

I'm going to close this because I don't think there is anything to do on the old doc

- Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/dotnet/docs-desktop/issues/1625*issuecomment-1593667938__;Iw!!HBnMciuwfVSXJQ!Qv-bK0Ng6X5sjDONrVPOsanzEddQ7M3sUZoph2Xc0hrYKEKvcEW707tinOUiEOp0iDBgOJHrfKjJyD7-xUDUwnZr$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AYNDUH6TSBGUVYCXUOHJWBDXLNUD5ANCNFSM6AAAAAAXF4JRHA__;!!HBnMciuwfVSXJQ!Qv-bK0Ng6X5sjDONrVPOsanzEddQ7M3sUZoph2Xc0hrYKEKvcEW707tinOUiEOp0iDBgOJHrfKjJyD7-xQ6UqR7d$. You are receiving this because you were mentioned.Message ID: @.**@.>>