如何制作 ObservableCollection的深层副本?在 Silverlight 4 中?

发布于 2024-09-12 20:28:13 字数 137 浏览 6 评论 0原文

我想在 Silverlight 4 中制作 ObservableCollection 的深层副本,以便检测列表中项目的更改。由于 ICloneable 在 SL 中不可用,我该如何执行此操作?

或者 - 是否有更好的方法来检查集合中项目的更改?

I want to make a deep copy of an ObservableCollection in Silverlight 4 in order to detect changes in the items in the list. Since ICloneable isn't available in SL how can I do this?

OR - is there a better way to check for changes in the items in the collection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

长亭外,古道边 2024-09-19 20:28:13

当然。如果您个人实现了 INotifyPropertyChanged,那么您可以注册并监听它。

基本上,您的各个项目应该实现更改的属性。下面是一个执行此操作的 Widget:

public class Widget : INotifyPropertyChanged
{
    private int _id;

    public int Id
    {
        get { return _id; }
        set
        {
            _id = value;
            RaisePropertyChanged("Id");
        }
    }

    private string _name;

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            RaisePropertyChanged("Name");
        }
    }



    private void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

接下来,您需要管理可观察集合并注册集合更改事件。添加小部件后,您将挂钩属性更改。当它们被移除时,你就解开了:

public class WidgetViewModel
{
    public WidgetViewModel()
    {
        Widgets = new ObservableCollection<Widget>();
        Widgets.CollectionChanged += Widgets_CollectionChanged;
    }

    void Widgets_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e != null && e.NewItems != null)
        {
            foreach (var widget in e.NewItems.OfType<Widget>())
            {
                widget.PropertyChanged += WidgetViewModel_PropertyChanged;
            }
        }

        if (e != null && e.OldItems != null)
        {
            foreach(var widget in e.OldItems.OfType<Widget>())
            {
                widget.PropertyChanged -= WidgetViewModel_PropertyChanged;
            }
        }
    }

    void WidgetViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        switch(e.PropertyName)
        {
            case "Id":
                break;
            case "Name":
                break;
            default:
                break;
        }
    }
    public ObservableCollection<Widget> Widgets { get; private set; }
}

在属性改变中,你可以处理发生的任何事情。

Sure. If your individual implement INotifyPropertyChanged, then you can register and listen for that.

Basically, your individual items should implement the property changed. Here is a Widget that does this:

public class Widget : INotifyPropertyChanged
{
    private int _id;

    public int Id
    {
        get { return _id; }
        set
        {
            _id = value;
            RaisePropertyChanged("Id");
        }
    }

    private string _name;

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            RaisePropertyChanged("Name");
        }
    }



    private void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

Next, you need to manage the observable collection and register to collection change events. When widgets are added, you hook into the property change. When they are removed, you unhook:

public class WidgetViewModel
{
    public WidgetViewModel()
    {
        Widgets = new ObservableCollection<Widget>();
        Widgets.CollectionChanged += Widgets_CollectionChanged;
    }

    void Widgets_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e != null && e.NewItems != null)
        {
            foreach (var widget in e.NewItems.OfType<Widget>())
            {
                widget.PropertyChanged += WidgetViewModel_PropertyChanged;
            }
        }

        if (e != null && e.OldItems != null)
        {
            foreach(var widget in e.OldItems.OfType<Widget>())
            {
                widget.PropertyChanged -= WidgetViewModel_PropertyChanged;
            }
        }
    }

    void WidgetViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        switch(e.PropertyName)
        {
            case "Id":
                break;
            case "Name":
                break;
            default:
                break;
        }
    }
    public ObservableCollection<Widget> Widgets { get; private set; }
}

In the property change, you can deal with whatever is going on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文