由绑定列表组成的 C# 绑定列表
有没有一种简单的方法可以让一个由多个绑定列表组成的绑定列表?即,这是列表的“视图”。
也就是说:我有3个列表(list1,list2,list3)。我想要一个始终是 3 个 listx 的并集的列表(我们可以假设 2 个不同的列表中不包含任何对象)。
当然,我可以成功使用 ListChange 属性,但也许有更聪明的方法来做到这一点?
Is there a simple way to have a bindinglist composed of several bindinglists? i.e. that is the "view" of the lists.
That is to say: I have 3 lists (list1,list2,list3). I want a list that is always the union of the 3 listx (we can suppose that no object is contained in 2 different lists).
Certainly, I can succeed in using the ListChange property but maybe there is a smarter way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您需要创建自己的类型,实现
IList
、IBindingList
(最好是IBindingListView
)以及可选的ICancelAddNew< /code> 和
IRaiseItemChangedEvents
。您还需要一个公共非对象索引器 (public T this[int index] {get;}
) 或ITypedList
。由于做过与此类似的事情,我强烈建议您;不要这样做,除非这确实很重要。将引用复制到新的
BindingList
中会更实用。还;与新项目;它会进入哪个列表?
To do this you would need to create your own type, implement
IList
,IBindingList
(and ideallyIBindingListView
), and optionallyICancelAddNew
andIRaiseItemChangedEvents
. You'd also need either a public non-object indexer (public T this[int index] {get;}
) orITypedList
.From having done things similar to this, I strongly advise you; don't, unless this is really important. It would be more pragmatic to copy the references into a new
BindingList<>
.Also; with new items; which list would it go into?
您是否研究过 CompositeCollection 类?
根据您尝试执行的操作,它可能会有所帮助:其目的是将多个集合合并为一个集合(通常用于显示/绑定目的)。因此,您可以创建一个 CompositeCollection 并向其中添加三个 BindingList 实例。
CompositeCollection
将自动更新以包含“子”列表的成员。Have you looked into the CompositeCollection class?
Depending on what you're trying to do, it might help: its purpose is to combine multiple collections into a single collection (typically for display/binding purposes). So, you could create a
CompositeCollection
and add your threeBindingList
instances to it. TheCompositeCollection
will automatically update to include the members of the "child" lists.