我有一个类:
public class A : INotifyPropertyChanged
{
public List<B> bList { get; set; }
public void AddB(B b)
{
bList.Add(b);
NotifyPropertyChanged("bList");
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
绑定 (UserControl 的 DataContext 是 A 类的一个实例):
<ListBox ItemsSource="{Binding Path=bList}" />
在添加新对象到列表后,元素会显示出来,但ListBox没有更新。
将列表更改为ObservableCollection并删除NotifyPropertyChanged处理程序后,一切都正常了。
为什么列表没能正常工作?