Skip to content

control.DataBindings.Remove\Clear does not unsubscribe from the INotifyPropertyChanged.PropertyChanged event in the WinForms app. #1679

@NatKite

Description

@NatKite

Hello!

I think that this is the compatibility bug. (.NetCore 3.0 preview8)
Build and run the code below at the ,Net Framework and at the .Net Core platforms and we will see a difference.

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace TestApp {
    class Program {
        public class MainObject : INotifyPropertyChanged {
            private string text;
            public string Text {
                get { return text; }
                set {
                    if(text != value) {
                        text = value;
                        if(PropertyChanged != null) {
                            PropertyChanged(this, new PropertyChangedEventArgs(nameof(Text)));
                        }
                    }
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;

            public bool IsPropertyChangedAssigned { get { return PropertyChanged != null; } }

        }

        static void Main(string[] args) {
            MainObject mainObject = new MainObject();
            mainObject.Text = "Test text";
            Form form = new Form();
            TextBox textBox = new TextBox();
            Binding binding = new Binding("Text", mainObject, "Text");
            textBox.DataBindings.Add(binding);
            textBox.Parent = form;
            form.Show();
            Console.WriteLine("MainObject.IsPropertyChangedAssigned: {0}", mainObject.IsPropertyChangedAssigned);
            textBox.DataBindings.Clear();
            binding = null;
            Console.WriteLine("MainObject.IsPropertyChangedAssigned: {0}", mainObject.IsPropertyChangedAssigned);
            Console.ReadLine();
        }
    }
}

For .Net Framework the result is:
MainObject.IsPropertyChangedAssigned: True
MainObject.IsPropertyChangedAssigned: False
For .Net Core the result is:
MainObject.IsPropertyChangedAssigned: True
MainObject.IsPropertyChangedAssigned: True

The PropertyChanged event subscription remains after binding remove.

Thanks,
Nat.

Metadata

Metadata

Assignees

No one assigned

    Labels

    🪲 bugProduct bug (most likely)💥 regression-previewRegression from a preview releasetenet-compatibilityIncompatibility with previous versions or with WinForms for .NET Framework

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions