我在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; }
}
在此之后我创建了两个列表。
static List<PropertyFieldsInExcel> listA = new List<PropertyFieldsInExcel>();
public List<Property> listB = new List<Property>();
现在,我想在这两个列表之间进行双向绑定。就像在listA
中某些内容发生变化时,listB
中的相应元素必须得到更新。
与if listA[i].ShortName = "abc"
一样,listB[i].ShortName
也必须具有相同的值。
listA.Add()
应触发listB.Add()
,反之亦然。
谢谢!
答案 0 :(得分:2)
答案 1 :(得分:-1)
在您的类中实现System.ComponentModel.INotifyPropertyChanged
- 接口,并使用PropertyChanged
事件处理程序在您的类中进行相应的更改。
使用System.Collections.ObjectModel.ObservableCollection<>
代替列表。