Rama
Rama

Reputation: 1069

How to bind variables of two classes in C#?

I have two similar classes in C#

public class Property
{
    public string version;               
    public string CCodeName { get; set; }
    public string CDoc { get; set; }
    public string ShortName { get; set; }
}

public class PropertyFieldsInExcel
{
    public string ShortNames { get; set; }
    public string CNames { get; set; }
    public string CDoc { get; set; }
    public string Version { get; set; }        
}   

After this I have created two lists.

static List<PropertyFieldsInExcel> listA = new List<PropertyFieldsInExcel>();
public List<Property> listB = new List<Property>();

Now, I want to have a two-way binding between these two lists. As in, whenever something changes in listA the corresponding element in listB must get updated.

Like if, listA[i].ShortName = "abc" then listB[i].ShortName also must have the same value. listA.Add() should trigger listB.Add() and vice versa.

Thank you!

Upvotes: 2

Views: 1632

Answers (2)

FoldFence
FoldFence

Reputation: 2802

Like @Amir said, you have to implement the INotifyPropertyChanged Class,

and your case is the exact example from: INotifyPropertyChanged

You should have a try on this example.

Upvotes: 3

Amir Shrestha
Amir Shrestha

Reputation: 451

Implement System.ComponentModel.INotifyPropertyChanged-Interface in your classes and use PropertyChanged event handler to make corresponding changes in your classes. Use System.Collections.ObjectModel.ObservableCollection<> instead of list.

Upvotes: -1

Related Questions