日期:2014-05-17 浏览次数:20762 次
class Program
{
static void Main(string[] args)
{
ObservableCollection<string> strs = new ObservableCollection<string>();
strs.Add("123");
strs.CollectionChanged += new NotifyCollectionChangedEventHandler(strs_CollectionChanged);
strs.Clear();
Console.Read();
}
private static void strs_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
Console.WriteLine("NEW");
}
if (e.OldItems != null)
{
Console.WriteLine("OLD");
}
}
}
clear时会执行下面
protected override void ClearItems()
{
this.CheckReentrancy();
base.ClearItems();
this.OnPropertyChanged("Count");
this.OnPropertyChanged("Item[]");
this.OnCollectionReset();
}
this.OnCollectionReset();源码是
private void OnCollectionReset()
{
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
NotifyCollectionChangedEventArgs的构造中并未对olditems做处理