gpt4 book ai didi

.net - base.OnPropertyChanged ("Item[]") 在 ObservableCollection 中做什么?

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

我在 ObservableCollection<T> 中四处寻找使用反编译器并看到一些好奇的OnPropertyChanged我以前从未见过的代码。

public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
private const string IndexerName = "Item[]";

protected override void ClearItems()
{
...
base.OnPropertyChanged("Count");
base.OnPropertyChanged("Item[]");
...
}
}

OnPropertyChanged("Item[]") 是什么意思?调用 do ,这对编写我自己的代码有什么帮助?

它必须做一些与标准不同的事情OnPropertyChanged调用,因为“Item”不是对象的属性,并且“[]” ' 当然不是“任何”属性名称的一部分。

最佳答案

调用OnPropertyChanged("Item[]")需遵循INotifyPropertyChanged精神。默认索引器返回的数据Item已改变。

在您的具体示例中,集合已被清除,因此如果您要索引到集合的特定项目,则需要通知您您感兴趣的对象引用可能不同。

编辑

在 Kevin 关于绑定(bind)到索引器的评论之后,我编写了一个应用程序来测试绑定(bind)。

我创建了一个ObservableCollection<int>并像这样填充:

this.Indexed.Add(1);
this.Indexed.Add(2);
this.Indexed.Add(3);

如果您通过像这样的索引器绑定(bind)到某些内容,它将显示 3 :

<TextBlock Text="{Binding Indexed[2]}" />

然后在运行时更改该索引处的对象,

this.Indexed.Insert(2, 10);

TextBlock将更新并显示新值 10 .

关于.net - base.OnPropertyChanged ("Item[]") 在 ObservableCollection 中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11871720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com