Binding to the CollectionChanged on child collection

Binding to the CollectionChanged on child collection

Old forum URL: forums.lhotka.net/forums/t/10063.aspx


JonM posted on Friday, February 11, 2011

I want to force a duplicate check rule to run on a child collection.  I think if I bind to the CollectionChanged and then fire a BusinessRules.CheckRules(MyChildCollectionProperty) it will work.  The problem is that I don't know where to bind to the CollectionChanged event.  I'm using Silverlight MVVM with dataportal.  I can't bind in the dataportal code because it will never fire.  Any help here would be great!

RockfordLhotka replied on Friday, February 11, 2011

If the list has a parent, I typically handle the ChildChanged event in the parent and loop through the child objects to trigger their rules.

If the list is the root, then you have to handle the CollectionChanged event in the list class itself, which is fine.

Either way, you need to hook the event in a safe manner. You might think to use the parent's constructor, but that isn't really safe because of differences between .NET and SL serialization.

The safe approach is to do it in two places: Initialize and OnDeserialized.

JonM replied on Tuesday, February 15, 2011

That worked!  I was wondering where to hook in for this.  Intialize and OnDeserialized worked.  Thanks!

Copyright (c) Marimer LLC