2

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!

Rama
  • 1,019
  • 1
  • 15
  • 34
  • I do not have any idea how to proceed. I'm kind of amateur in C#. Any kind of solution is acceptable to me. – Rama Jun 02 '16 at 08:17

2 Answers2

3

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.

FoldFence
  • 2,674
  • 4
  • 33
  • 57
  • I went through the code in the link. Similar code would work if I had just two classes to be bound. But I have to bind a list of objects and also insert or add just like lists. That is where I'm facing issues. – Rama Jun 02 '16 at 10:20
  • Mhm did you tried to change the Lists to ObservableCollections because they automatically update if an Object removes add etc. here is a similar question how to sync two observable collections: https://social.msdn.microsoft.com/Forums/en-US/2e278e3c-27ab-42b5-8a7b-6828ddbb9caf/how-to-sync-two-observable-collection-?forum=wpf – FoldFence Jun 02 '16 at 10:30
-1

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.

DIF
  • 2,470
  • 6
  • 35
  • 49
Amir Shrestha
  • 451
  • 5
  • 11