跟踪属性更改中的更改

发布于 2024-08-30 10:07:11 字数 847 浏览 3 评论 0原文

我想将我的可观察视图模型对象集合过滤为更新的对象。我已经订阅了每个视图模型的属性更改事件。但我不确定如何跟踪对象,因此最终结果将只是在 UI 中更新的对象。

    ProgramViewModel Cur=new ProgramViewModel(prg);
                        Cur.PropertyChanged += new PropertyChangedEventHandler(Cur_PropertyChanged);
                        program.Add(Cur);
//here program is my observable collection of viewmodels

 void Cur_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {  
          //What will be the code that will filter out only updated objects
            throw new NotImplementedException();
        }

//Now in my Save Event handler

private void Save_Click(object sender, RoutedEventArgs e)
        {
          foreach (ProgramViewModel model in program)
            {
                //I need only updated objects here to be added to database
            }
       }

I want to filter my observable collection of viewmodel objects to the objects that are updated. I have subscribed to the Property Changed event for each viewmodel. But I am not sure how shall I track the objects so the end result would be only the objects that are updated in the UI.

    ProgramViewModel Cur=new ProgramViewModel(prg);
                        Cur.PropertyChanged += new PropertyChangedEventHandler(Cur_PropertyChanged);
                        program.Add(Cur);
//here program is my observable collection of viewmodels

 void Cur_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {  
          //What will be the code that will filter out only updated objects
            throw new NotImplementedException();
        }

//Now in my Save Event handler

private void Save_Click(object sender, RoutedEventArgs e)
        {
          foreach (ProgramViewModel model in program)
            {
                //I need only updated objects here to be added to database
            }
       }

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

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

发布评论

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

评论(1

我ぃ本無心為│何有愛 2024-09-06 10:07:11

我只是将它们存储在 HashSet 中。这样,您就不必担心跟踪集合中已有的内容:

private readonly HashSet<ProgramViewModel> changedPrograms = new HashSet<ProgramViewModel>();

void Cur_PropertyChanged(object sender, PropertyChangedEventArgs e)
{  
    changedPrograms.Add((ProgramViewModel)sender);
}

private void Save_Click(object sender, RoutedEventArgs e)
{
    foreach (ProgramViewModel model in changedPrograms)
    {
        ...
    }
}

I would just store them in a HashSet<ProgramViewModel>. That way, you won't have to worry about tracking which ones are already in the collection:

private readonly HashSet<ProgramViewModel> changedPrograms = new HashSet<ProgramViewModel>();

void Cur_PropertyChanged(object sender, PropertyChangedEventArgs e)
{  
    changedPrograms.Add((ProgramViewModel)sender);
}

private void Save_Click(object sender, RoutedEventArgs e)
{
    foreach (ProgramViewModel model in changedPrograms)
    {
        ...
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文