.NET 4.0 索引器与 ObservableCollection
带有索引器的 ObservableCollection 的 .NET C# 语法是什么?我想要一个 ObservableCollection 并按序号位置或字符串名称引用项目。我知道您使用它来表示索引器,但我不知道如何将其放入 ObservableCollection 中。谢谢
谢谢4位的回答。我知道如何创建和 ObservableCollection,并且我知道如何创建索引器。我不知道如何将它们结合起来。我要求提供具有序数和字符串索引的 ObservableCollection 的示例代码。 再次感谢
What is the .NET C# syntax for an ObservableCollection with an indexer? I would like an ObservableColletion and refer to the items by ordinal position or a string name. I know you the use this to denote an indexer but I don't know how to put that in an ObservableCollection. Thanks
Thanks for the 4 answers. I know how create and ObservableCollection and I know how to create an indexer. I don't know how to combine them. I am asking for sample code for an ObservableCollection with an ordinal and string index.
Thank again
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ObservableCollection 继承自 Collection,因此它已经具有基于位置的索引。
对于基于字符串的索引,您可以查看人们对 ObservableDictionary 的实现。
就我个人而言,为了获得更好的性能,我创建了一个从 ObservableCollection 派生的 HashedObservableCollection,其中包含索引键的字典以加快查找时间。通过重写 InsertItem、RemoveItem 和 ClearItems,您可以保持字典同步。
在我的示例中,键可以是任何类型,但我们假设键永远不会改变 - 如果替换某个项目,则会将其替换为具有相同键的对象。如果你想简化这个,你可以用 String 替换 TKey。
代码:
ObservableCollection inherits from Collection, so it already has position-based indexing.
For string-based indexing, you can look into peoples implementations of ObservableDictionary.
Personally, for better performance, I've created a HashedObservableCollection deriving from ObservableCollection which contains a Dictionary of keys to indexes to speed lookup time. By overriding InsertItem, RemoveItem, and ClearItems, you keep the dictionary in sync.
In my example, the keys can be of any type but we assume the key never changes - if an item is replaced, it is replaced with an object with the same key. If you want to simplify this, you can replace TKey with String.
Code:
这是我的想法,希望这有助于找到您的解决方案
here is my idea, hope this helps to find your solution
如果我正确理解您的问题,您可以使用此示例
http://msdn.microsoft .com/en-us/library/ms132434.aspx
If i understand your question correctly, you can use this example
http://msdn.microsoft.com/en-us/library/ms132434.aspx
我认为这是您正在寻找的语法:
ObservableCollection
的文档:http://msdn.microsoft.com/en-us/library/ms668604.aspx索引器文档(又名“项目”)属性): http://msdn.microsoft.com/en-us/library/ ms132434.aspx
根据您的评论,听起来您正在寻找
ObservableDictionary
而不是ObservableCollection
。 .NET 没有内置这样的集合,但快速谷歌搜索发现了这两个实现:http://blogs.microsoft.co.il/blogs/shimmy/archive/2010/12/26/observabledictionary-lt-tkey-tvalue-gt-c.aspx
http://uberpwn.wordpress.com/2011/03/12/my-c-observabledictionary/
I think this is the syntax that you are looking for:
Documentation for
ObservableCollection<T>
: http://msdn.microsoft.com/en-us/library/ms668604.aspxDocumentation for the Indexer (aka the 'Item' property): http://msdn.microsoft.com/en-us/library/ms132434.aspx
Based on your comments it sounds like you are looking for an
ObservableDictionary
as opposed to anObservableCollection
. .NET does not have such a collection built in but a quick google search found these two implementations:http://blogs.microsoft.co.il/blogs/shimmy/archive/2010/12/26/observabledictionary-lt-tkey-tvalue-gt-c.aspx
http://uberpwn.wordpress.com/2011/03/12/my-c-observabledictionary/