ObservableCollection 中 ViewModel 的 PropertyChanged 事件

发布于 2024-08-30 20:26:09 字数 95 浏览 3 评论 0原文

我有一个可观察的视图模型对象集合。如何在创建集合中的每个视图模型时订阅它们的 Property Changed 事件并跟踪哪些视图模型已更改,以便我可以将它们更新到我的数据库。

I have a observable collection of viewmodel objects. How can I subscribe to the Property Changed event of each view model in my collection as they are created and track which ones have been changed, so that I can updated them to my database.

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

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

发布评论

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

评论(1

月棠 2024-09-06 20:26:09

我相信下面的代码可以作为如何解决您的问题的示例。在此示例中,MyCollection 是 ViewModel 对象的属性。 ViewModel 实现 INotifyPropertyChanged 接口。

private void AddCollectionListener()
    {
        if (MyCollection != null)
        {
            MyCollection.CollectionChanged += 
                new System.Collections.Specialized.NotifyCollectionChangedEventHandler(MyCollection_CollectionChanged);
        }
    }

    void MyCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        // Remove Listeners to each item that has been removed
        foreach (object oldItem in e.OldItems)
        {
            ViewModel viewModel = oldItem as ViewModel;

            if (viewModel != null)
            {
                viewModel.PropertyChanged -= viewModel_PropertyChanged;
            }
        }

        // Add Listeners to each item that has been added
        foreach (object newItem in e.NewItems)
        {
            ViewModel viewModel = newItem as ViewModel;

            if (viewModel != null)
            {
                viewModel.PropertyChanged += new PropertyChangedEventHandler(viewModel_PropertyChanged);
            }
        }
    }

    void viewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        // TODO: Property Changed Logic

        switch (e.PropertyName)
        {
            case "MyPropertyName":
                // TODO: Perform logic necessary when MyPropertyName changes
                break;
            default:
                // TODO: Perform logic for all other property changes.
                break;
        }
    }

I believe that the code below serves as an example of how to solve your problem. In this example MyCollection is a property ViewModel objects. ViewModel implements the INotifyPropertyChanged interface.

private void AddCollectionListener()
    {
        if (MyCollection != null)
        {
            MyCollection.CollectionChanged += 
                new System.Collections.Specialized.NotifyCollectionChangedEventHandler(MyCollection_CollectionChanged);
        }
    }

    void MyCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        // Remove Listeners to each item that has been removed
        foreach (object oldItem in e.OldItems)
        {
            ViewModel viewModel = oldItem as ViewModel;

            if (viewModel != null)
            {
                viewModel.PropertyChanged -= viewModel_PropertyChanged;
            }
        }

        // Add Listeners to each item that has been added
        foreach (object newItem in e.NewItems)
        {
            ViewModel viewModel = newItem as ViewModel;

            if (viewModel != null)
            {
                viewModel.PropertyChanged += new PropertyChangedEventHandler(viewModel_PropertyChanged);
            }
        }
    }

    void viewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        // TODO: Property Changed Logic

        switch (e.PropertyName)
        {
            case "MyPropertyName":
                // TODO: Perform logic necessary when MyPropertyName changes
                break;
            default:
                // TODO: Perform logic for all other property changes.
                break;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文