I'm asking this question in relation to a BusinessListBase collection bound to Silverlight grid control.
The collection has FromDate and ToDate properties.
The BusinessBase items within the collection have a Date property, with a business rule to check if the Date is between the From and To dates. The rule only runs when the item parent property is the collection.
The grid has an insert function where by it creates a new instance of an item and adds it to the collection, I would like the Date validation to run at this point so the UI is updated and notifies the user if the Date value is valid or not. In the collection I overrode OnCollectionChanged to run the item business rule whenever it was added to the collection. The main problem I found was that OnCollectionChanged was being called as a result of deserialization and during the fetch method of my factory object.
My solution follows but I'm thinking that there must be a better technique?
protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (this.RaiseListChangedEvents) // don't call during DataPortal.Fetch or Deserialization (requires code in OnSetChildren override)
{
if ((e.Action == NotifyCollectionChangedAction.Add)
|| (e.Action == NotifyCollectionChangedAction.Replace)
|| (e.Action == NotifyCollectionChangedAction.Reset))
{
foreach (BBEdit item in e.NewItems)
{
item.CheckRulesRequiringParentCollection();
}
}
}
base.OnCollectionChanged(e);
}
protected override void OnSetChildren(Csla.Serialization.Mobile.SerializationInfo info, Csla.Serialization.Mobile.MobileFormatter formatter)
{
var originalRaiseListChangedEvents = this.RaiseListChangedEvents;
try
{
this.RaiseListChangedEvents = false;
base.OnSetChildren(info, formatter);
}
finally
{
this.RaiseListChangedEvents = originalRaiseListChangedEvents;
}
}
Regards
Peran
I don't know for sure about a better answer (though I suspect you might be better off overriding the collection method that runs when an item is added?).
But I did add a wish list item to suppress raising of the event, for perf reasons if nothing else: http://www.lhotka.net/cslabugs/edit_bug.aspx?id=935
Copyright (c) Marimer LLC