I have an object that I attached to a control using C # WinForms (target .NET 4.5.2). I implemented INotifyPropertyChanged , and when I change the Property of this object, it, as expected, updates the form control. However, when I change an instance of an object to a new instance, it will no longer update the control, even if I try to change a specific property.
class User : INotifyPropertyChanged { string _name = ""; public string Name { get { return _name; } set { _name = value; OnPropertyChanged("Name"); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string property) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); } public User() { } public User(string name) { Name = name; } public User(User otherUser) { Name = otherUser.Name; } }
and in form I
User currentUser = new User("Example Name"); lblName.DataBindings.Add("Text", currentUser, "Name");
which is updated properly. I can change the name using currentUser.Name = "Blahblah"; and it works great. When I try to call currentUser = new User("New Name"); , it will no longer update, no matter what I do.
I understand that a particular instance of an object is what the controls are attached to. My main goal is not to manually go through each property of larger objects and manually change everything every time I want to change instances.
My question is: is there a way to change instances of an object without unlinking the control?
source share