pfthroaway pfthroaway - 10 months ago 77
C# Question

WinForms Data Binding instance

I have an object that I have bound to a control on a form using C# WinForms (targetting .NET 4.5.2). I have implemented

, and when I modify a Property of this object, it updates on the Form's control as expected. However, when I change this object's instance to a new instance, it will no longer update the control, even if I try to modify 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 on the Form I have

User currentUser = new User("Example Name");
lblName.DataBindings.Add("Text", currentUser, "Name");

which updates properly. I can change name using
currentUser.Name = "Blahblah";
and it works fine. When I try to call
currentUser = new User("New Name");
, it will no longer update no matter what I do.

It is my understanding that the specific instance of the object is what the controls are bound to. My primary goal is to not have to manually go through each Property of larger objects and manually change everything over every single time I want to change instances.

My question: is there a way to change instances of an object without removing the binding to the control?

Answer Source

To get the desired behavior, you should not bind directly to the concrete instance like you do currently:

lblName.DataBindings.Add("Text", currentUser, "Name");

Instead, you need some intermediary. The easiest is to use the BindingSource component for that purpose.

Add a BindingSource field to the form:

private BindingSource dataSource;

Then initialize it and bind the controls to it one time (usually in form Load event):

dataSource = new BindingSource { DataSource = typeof(User) };
lblName.DataBindings.Add("Text", dataSource, "Name");
// ...

Now anytime you want to bind to a User instance, you simply assign it to the DataSource property of the BindingSource:


dataSource.DataSource = new User("Example Name");


dataSource.DataSource = new User("New Name");