追踪物业变化的变化

时间:2010-04-29 20:27:01

标签: c# wpf inotifypropertychanged

我想将可观察的viewmodel对象集合过滤到更新的对象。我已为每个viewmodel订阅了Property Changed事件。但我不确定如何跟踪对象,因此最终结果将只是在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
            }
       }

1 个答案:

答案 0 :(得分:2)

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

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)
    {
        ...
    }
}