gpt4 book ai didi

c# - BindingList INotifyPropertyChanged 意外行为

转载 作者:行者123 更新时间:2023-11-30 14:12:36 24 4
gpt4 key购买 nike

假设,我有对象:

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。在查看属性“数据”中,我发现我的事件 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;
}
}

我哪里错了?或者这是已知的错误,我需要找到一些解决方法?谢谢!

最佳答案

BindingList<T>不检查每个特定项目是否实现 INotifyPropertyChanged .相反,它会检查一次通用类型参数。所以如果你的 BindingList<T>声明如下:

private BindingList<ITest> _listTest1;

然后ITest应该继承自INotifyPropertyChanged为了得到BindingList提高 ItemChanged事件。

关于c# - BindingList<T> INotifyPropertyChanged 意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17039296/

24 4 0