Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Binding doesn't work when I assign a new object instance to the bound variable

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 INotifyPropertyChanged, 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?

like image 780
pfthroaway Avatar asked Oct 02 '16 12:10

pfthroaway


People also ask

How do I bind data in XAML?

Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.

Which class is used for data binding in WPF?

WPF data binding supports data in the form of CLR objects and XML. To provide some examples, your binding source may be a UIElement, any list object, a CLR object that is associated with ADO.NET data or Web Services, or an XmlNode that contains your XML data.

How many types of data binding are there in Ado net?

There are two types of data-binding in ASP.NET namely, simple data-binding and declarative data binding. Some controls support both types while the rest support atleast simple data binding.

What is data binding in C?

Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically.


1 Answers

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:

initial:

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

later:

dataSource.DataSource = new User("New Name"); 
like image 130
Ivan Stoev Avatar answered Sep 23 '22 12:09

Ivan Stoev