2

我在 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"thenlistB[i].ShortName也必须具有相同的值。 listA.Add()应该触发listB.Add(),反之亦然。

谢谢!

4

2 回答 2

2

就像@Amir 说的,你必须实现 INotifyPropertyChanged 类,

你的情况就是来自:INotifyPropertyChanged

你应该试试这个例子。

于 2016-06-02T09:56:02.087 回答
-1

在您的类中实现System.ComponentModel.INotifyPropertyChanged-Interface 并使用PropertyChanged事件处理程序在您的类中进行相应的更改。使用System.Collections.ObjectModel.ObservableCollection<>而不是列表。

于 2016-06-02T07:55:03.477 回答