Reputation: 5478
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
}
}
Upvotes: 1
Views: 1267
Reputation: 30498
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)
{
...
}
}
Upvotes: 2