Silverlight 中带键的 ObservableCollection

发布于 2024-08-05 23:58:52 字数 688 浏览 9 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许你一世情深 2024-08-12 23:58:52

如果我理解正确,您需要创建一个具有 propertychanged 实现的 KeyValuePair 并将其与 ObservableCollection 一起使用

ObservableCollection< KeyValuePair<string,string>> 

public class KeyValuePair<TKey, TValue> : INotifyPropertyChanged
{
    public KeyValuePair(TKey key, TValue value)
    {
        _key = key;
        _value = value;
    }

    public TKey Key  { get {  return _key; } }

    public TValue Value { get  { return _value; }
        set
        {
            _value = value;
            NotifyPropertyChanged("Value");
        }
    }


    private TKey _key;
    private TValue _value;

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}

If I am understanding correctly, you need to create a KeyValuePair with propertychanged implementation and use that with ObservableCollection

ObservableCollection< KeyValuePair<string,string>> 

public class KeyValuePair<TKey, TValue> : INotifyPropertyChanged
{
    public KeyValuePair(TKey key, TValue value)
    {
        _key = key;
        _value = value;
    }

    public TKey Key  { get {  return _key; } }

    public TValue Value { get  { return _value; }
        set
        {
            _value = value;
            NotifyPropertyChanged("Value");
        }
    }


    private TKey _key;
    private TValue _value;

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}
尤怨 2024-08-12 23:58:52

ObservableDictionary(Of TKey, TValue) - VB.NET

常规功能列表:

  • ObservableDictionary(Of TKey, TValue)
  • AddRange 仅收到一次通知。
  • Generic EventArgs(Of TKey, TValue)
  • NotifyDictionaryChanging(Of TKey, TValue) - CancelEventArgs 的子类,允许取消操作。

ObservableDictionary(Of TKey, TValue) - VB.NET

General feature list:

  • ObservableDictionary(Of TKey, TValue)
  • AddRange getting notified only once.
  • Generic EventArgs(Of TKey, TValue)
  • NotifyDictionaryChanging(Of TKey, TValue) - a subclass of CancelEventArgs that allows cancelling operation.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文