Silverlight 中带键的 ObservableCollection
我想在 Silverlight 中实现一个带键的可观察集合,它将基于名为 Name 的属性存储唯一的对象。一种方法是使用 ObservableCollectionEx(另一篇 stackoverflow 帖子中的示例)类,该类订阅所包含元素上的所有 PropertyChanged 事件,并检查 name 属性是否发生更改。更好的是,创建我自己的事件,该事件将告诉我名称属性已更改,并在项目已存在时抛出 ValidationException。我不一定想使用索引器 this[Name] 检索对象。
像这样的事情:
private string name;
public string Name
{
get { return name; }
set {
if (value != name)
{
OnNameChanged();
name = value;
OnPropertyChanged("Name");
}
}
}
还有其他更优雅的解决方案吗?简单多了? 谢谢, 阿德里安
P.S.我知道还有一个由 Wpf 博士整理的 ObservableDictionary,并且很容易将其移动到 Silvelight,但我不知道如何将它与 DataForm 等一起使用。
I want to implement a keyed observable collection in Silverlight, that will store unique objects based on a property called Name. One way of doing it is to use the ObservableCollectionEx (samples in another stackoverflow post) class which subscribes to all PropertyChanged events on the contained elemens and check to see if the name property changes. Better yet, create my own event that will tell me the name attribute changed and throw a ValidationException if item already exists. I don't necessarily want to retrive the object with an indexer this[Name].
somthing like this:
private string name;
public string Name
{
get { return name; }
set {
if (value != name)
{
OnNameChanged();
name = value;
OnPropertyChanged("Name");
}
}
}
Is there another solution more elegant? Much simpler?
Thanks,
Adrian
P.S. I know there is also an ObservableDictionary that Dr Wpf put together and it's easy to move it to Silvelight, but I don't know how to use it with DataForm and such.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确,您需要创建一个具有 propertychanged 实现的 KeyValuePair 并将其与 ObservableCollection 一起使用
If I am understanding correctly, you need to create a KeyValuePair with propertychanged implementation and use that with ObservableCollection
ObservableDictionary(Of TKey, TValue) - VB.NET
常规功能列表:
ObservableDictionary(Of TKey, TValue) - VB.NET
General feature list: