大佬教程收集整理的这篇文章主要介绍了c# – BindingList INotifyPropertyChanged意外行为,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
public interface ITest { String Data { get; set; } } public class Test1 : ITest,INotifyPropertyChanged { private String _data; public String Data { get { return _data; } set { if (_data == value) return; _data = value; OnPropertyChanged("Data"); } } protected void OnPropertyChanged(String propertyName) { var h = PropertyChanged; if (null != h) h(this,new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; }
及其持有人:
private BindingList<ITest> _listTest1; public BindingList<ITest> ListTest1 { get { return _listTest1 ?? (_listTest1 = new BindingList<ITest>() { RaiseListChangedEvents = true }); } }
另外,我订阅了ListChangedEvent
public MainWindow() { InitializeComponent(); ListTest1.ListChanged += new ListChangedEventHandler(ListTest1_ListChanged); } void ListTest1_ListChanged(object sender,ListChangedEventArgs E) { messageBox.Show("ListChanged1: " + e.ListChangedTypE); }
和2个测试处理程序:
用于添加对象
private void AddITestHandler(object sender,RoutedEventArgs E) { ListTest1.Add(new Test1 { Data = Guid.NewGuid().ToString() }); }
并改变
private void ChangeITestHandler(object sender,RoutedEventArgs E) { if (ListTest1.Count == 0) return; ListTest1[0].Data = Guid.NewGuid().ToString(); //if (ListTest1[0] is INotifyPropertyChanged) // messageBox.Show("really pch"); }
ItemAdded发生,但ItemChanged没有.在seeTing proprty里面“Data”我发现我的事件PropertyChanged没有订阅者:
protected void OnPropertyChanged(String propertyName) { var h = PropertyChanged; // h is null! why?? if (null != h) h(this,new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged;
深入挖掘我采用反射器并发现BindingList:
protected override void InsertItem(int index,T item) { this.EndNew(this.addNewPos); base.InsertItem(index,item); if (this.raiseItemChangedEvents) { this.HookPropertyChanged(item); } this.FireListChanged(ListChangedType.ItemAdded,indeX); } private void HookPropertyChanged(T item) { INotifyPropertyChanged changed = item as INotifyPropertyChanged; if (changed != null) // Its seems like null reference! really?? { if (this.propertyChangedEventHandler == null) { this.propertyChangedEventHandler = new PropertyChangedEventHandler(this.Child_PropertyChanged); } changed.PropertyChanged += this.propertyChangedEventHandler; } }
以上是大佬教程为你收集整理的c# – BindingList INotifyPropertyChanged意外行为全部内容,希望文章能够帮你解决c# – BindingList INotifyPropertyChanged意外行为所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。