.NET 4.0索引器,具有ObservableCollection
带索引器的ObservableCollection的.NET C#语法是什么?我想要一个ObservableCollection,并通过序号位置或字符串名称引用项目。我知道你用它来表示索引器,但我不知道如何把它放在一个可观察的集合中。谢谢 谢谢你的4个答案。我知道如何创建和观察收集,我知道如何创建索引器。我不知道如何把它们结合起来。我要求提供带有序号和字符串索引的ObservableCollection的示例代码。.NET 4.0索引器,具有ObservableCollection,.net,observablecollection,indexer,.net,Observablecollection,Indexer,带索引器的ObservableCollection的.NET C#语法是什么?我想要一个ObservableCollection,并通过序号位置或字符串名称引用项目。我知道你用它来表示索引器,但我不知道如何把它放在一个可观察的集合中。谢谢 谢谢你的4个答案。我知道如何创建和观察收集,我知道如何创建索引器。我不知道如何把它们结合起来。我要求提供带有序号和字符串索引的ObservableCollection的示例代码。 再次感谢如果我正确理解您的问题,您可以使用此示例 如果我正确理解您的问题,您可以
再次感谢如果我正确理解您的问题,您可以使用此示例
如果我正确理解您的问题,您可以使用此示例
我想这就是您要寻找的语法:
// create a generic ObservableCollection - I used object, but you can use any Type
var collection = new ObservableCollection<object>();
// set the item at the index.
collection[0] = new object();
//创建一个通用的ObservableCollection-我使用了对象,但您可以使用任何类型
var collection=新的ObservableCollection();
//在索引处设置项目。
集合[0]=新对象();
可观察收集的文档
:
索引器的文档(也称为“项”属性):
根据您的评论,您似乎在寻找一个可观察的医疗机构
,而不是可观察的医疗机构
。NET没有内置这样的集合,但通过快速的谷歌搜索发现了以下两种实现:
// create a generic ObservableCollection - I used object, but you can use any Type
var collection = new ObservableCollection<object>();
// set the item at the index.
collection[0] = new object();
//创建一个通用的ObservableCollection-我使用了对象,但您可以使用任何类型
var collection=新的ObservableCollection();
//在索引处设置项目。
集合[0]=新对象();
可观察收集的文档
:
索引器的文档(也称为“项”属性):
根据您的评论,您似乎在寻找一个可观察的医疗机构
,而不是可观察的医疗机构
。NET没有内置这样的集合,但通过快速的谷歌搜索发现了以下两种实现:
使用系统;
使用System.Linq;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
命名空间foson.Utils
{
///
///表示由字典索引以提高查找/替换性能的BindableCollection。
///
///
///假定密钥不会更改,并且对于集合中的每个元素都是唯一的。
///集合不是线程安全的,所以调用应该是单线程的。
///
///BindableCollection中包含的元素类型
///索引键的类型
公共类HashedBindableCollection:ObservableCollection
{
受保护的内部字典索引=新字典();
受保护的内部功能键选择器;
///
///创建新的HashedBindableCollection
///
///用于从值创建键的选择器函数
公共HashedBindableCollection(Func键选择器)
:base()
{
如果(keySelector==null)抛出新的ArgumentException(“keySelector”);
_键选择器=键选择器;
}
#区域保护方法
受保护的覆盖无效插入项(int索引,TValue项)
{
变量键=_键选择器(项);
if(索引容器(键))
抛出新的DuplicateKeyException(key.ToString());
如果(索引!=此.Count)
{
foreach(indicies.Keys.Where(k=>indicies[k]>=index.ToList()中的var k)
{
指数[k]++;
}
}
基本插入项(索引,项目);
索引[索引]=索引;
}
受保护的覆盖无效ClearItems()
{
base.ClearItems();
清晰的;
}
受保护的覆盖void removietem(int索引)
{
var项目=该[指数];
变量键=_键选择器(项);
基本删除项(索引);
删除(键);
foreach(indicies.Keys.Where(k=>indicies[k]>index.ToList()中的var k)
{
索引[k]——;
}
}
#端区
公共虚拟bool ContainsKey(TKey)
{
返回索引。ContainsKey(键);
}
///
///获取或设置具有指定键的元素。如果设置新值,则新值必须具有相同的键。
///
///更换元件的键
///
公共虚拟TValue此[TKey]
{
获取{返回此[indices[key]];}
设置
{
//确认密钥匹配
if(!\u键选择器(值).Equals(键))
抛出新的InvalidOperationException(“新值的键不匹配”);
如果(!indices.ContainsKey(键))
{
增加(价值);
}
其他的
{
此[索引[键]]=值;
}
}
}
///
///用新val替换给定键处的元素
using System.Collections.ObjectModel;
namespace WPFValidation
{
public class CustomObservableCollection<T> : ObservableCollection<T>
{
public T this[string key] {
get {
// you must implement some code to do this one
T item = GetItemWithAKey(key);
return item;
}
set {
T item = GetItemWithAKey(key);
if (item != null) {
// set the given value toi the item
this.SetItemValue(item, value);
}
}
}
private T GetItemWithAKey(string key) {
// find the item with teh given key
return default(T);
}
}
public class TestClass
{
public TestClass() {
var coll = new CustomObservableCollection<CustomKeyedClass>();
coll.Add(new CustomKeyedClass("One"));
coll.Add(new CustomKeyedClass("Two"));
var item = coll["One"];
var item2 = coll[1];
}
}
}