如何在 C# 4.0 中检测对通用列表的添加?
我有一个名为 LocationList
的 List
子类。这是我们添加其他属性(例如 IsExpanded
等)的便捷方法,这样我们就可以在 UI 中使用。足够好了。但是现在,我们希望每个位置都知道其父位置。因此,当某些内容添加到 LocationList 时,我们需要收到通知,但您无法覆盖 Add
和 Remove
虽然我们可以使用 ObservableCollection,但这更多的是 View/ViewModel。这是纯模型。 当然,
我们可以在将其添加到集合时手动设置父级,但我讨厌这样的非自动化逻辑,因为没有什么可以正确执行其设置,让集合(好吧,列表)自动说“嘿......你”。 '正在被添加,所以我会将你的父级设置为拥有我的类。”这就是我所追求的。
我的想法是代替子类 List
而只是创建一个使用直接类在内部列出
,然后我可以简单地公开我自己的添加/删除方法并处理其中对“父级”的调整。但是,这样做会破坏枚举内部集合的能力,因为它位于内部。
也就是说,有没有什么方法可以监听 List
的更改,或者至少将其 IEnumerable
接口从包装对象委托给其包装器?
I have a subclass of List<Location>
called LocationList
. This is a convenient way for us to add other properties (like IsExpanded
and such that we can use in the UI. Good enough. But now, we want each location to be aware of its parent. As such, we need to be notified when something is added to LocationList, but you can't override Add
and Remove
. While we can use ObservableCollection, that's more of a View/ViewModel construct. This is pure model data.
Sure we could manually set the parent when adding it to the collection, but I hate non-automated logic like that as there's nothing to enforce its set correctly. Letting the collection (well, List) automatically say 'Hey... you're being added, so I'll set your parent to the class that owns me." is what I'm after.
My thought is to instead of subclass List<Location>
to instead just create a straight class that uses a List<Location>
internally, and then I can simply expose my own Add/Remove methods and handle the adjustments to 'Parent' in there. However, doing so breaks being able to enumerate over the internal collection since it's inside.
That said, is there any way to either listen to changes to List<Location>
, or at least delegate its IEnumerable
interface from the wrapped object to its wrapper?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将您的 LocationList 更改为从
Collection
继承。我不知道为什么List
没有密封,但它不可扩展。来源:框架设计指南,第二版,第 251 页:
Change your LocationList to inherit from
Collection<Location>
instead. I don't know whyList<T>
isn't sealed, but it's not extendable.Source: Framework Design Guidelines, Second Edition, Page 251:
List
具有受保护的成员InsertItem
、RemoveItem
等,您可以在派生类中重写这些成员以执行您的操作想。** 更新 **
实际上上面是不正确的,它是
Collection
具有这些受保护的方法。一般来说,在派生自定义 List 类时,建议从Collection
派生,而不是List
。请参阅这个答案。
List<T>
has protected membersInsertItem<T>
,RemoveItem<T>
etc that you can override in your derived class to do what you want.** UPDATE **
Actually the above is incorrect, it's
Collection<T>
that has these protected methods. In general, when deriving custom List classes, it is recommended to derive fromCollection<T>
rather thanList<T>
.See this answer.
您仍然可以在自定义集合上实现
IEnumerable
。使用List<>
作为类的内部实现细节,并且您可以允许通过接口进行枚举。You can still implement
IEnumerable<Location>
on your custom collection. Use theList<>
as the internal implementation detail of the class, and you can allow enumeration via the interface.尝试通用的
BindingList
,添加和删除项目时会引发事件。Try the generic
BindingList<T>
, which raises events when items are added and removed.假设您遵循正确的技术并实现 IEnumerable 和 ICollection ,情况并非如此
,但我会使用 Observable 集合而不是重新发明轮子。
当然,您可以使用反应式扩展来完成您需要的操作。
This is not true assuming you follow proper technique and implement
IEnumerable
andICollection
But I would use Observable collection instead of reinventing the wheel.
And of course you can use Reactive extensions to do what you need.
您可以使用 观察者模式
You can either using Observer pattern