Event when child added to listEvent when child added to list
Old forum URL: forums.lhotka.net/forums/t/7850.aspx
Fele posted on Thursday, October 22, 2009
Does Csla raise event when a new child is added to list of children?
Thanks
Fele
RockfordLhotka replied on Thursday, October 22, 2009
Yes, this is a standard behavior of .NET collections like BindingList<T> (which is the base class for all CSLA lists). The event is called ListChanged.
On Silverlight the base class is ObservableCollection<T> and the event is called CollectionChanged.
RockfordLhotka replied on Thursday, October 22, 2009
Oh, also...
If you have an object structure like OrderEdit -> LineItems -> LineItem
and if you want code in OrderEdit to know that a new item was added to the list, your easiest option is to override OnChildChanged() - don't mess with events at all because they require hooking and unhooking and rehooking, where an override is just a lot simpler.
Fele replied on Thursday, October 22, 2009
Ok. I see it.
OnChildChanged was risen but I can't figure out how to find out if it is adding, deleting or updating child.
Thanks
Fele replied on Thursday, October 22, 2009
I will appreciate if you let me know if this is correct approach.
protected override void OnChildChanged(ChildChangedEventArgs e)
{
base.OnChildChanged(e);
if (e.PropertyChangedArgs != null)
{
switch (e.PropertyChangedArgs.PropertyName)
{
case "X1":
CalcTotalAmt();
break;
default:
break;
}
}
else
{
if (e.ChildObject.ToString() == "ObjectList")
{
//check if added
if (e.ListChangedArgs.ListChangedType == ListChangedType.ItemAdded)
{
UpdateOtherObj();
}
}
}
}
Only problem that I have my client classes linked to server ones and ListChangeType generate two errors:
1)'System.Collections.Specialized.NotifyCollectionChangedEventArgs' does not contain a definition for 'ListChangedType' and no extension method 'ListChangedType' accepting a first argument of type 'System.Collections.Specialized.NotifyCollectionChangedEventArgs' could be found (are you missing a using directive or an assembly reference?)
2) The name 'ListChangedType' does not exist in the current context
If I open this class on server side, it is OK.
On Client, I'm getting above errors.
RockfordLhotka replied on Thursday, October 22, 2009
Yes, that’s basically the idea – use the properties of
ChildChangedEventArgs to determine what happened to what object.
I realize there are differences between SL and .NET – that’s
because collections aren’t the same on the two platforms. The easiest answer is
probably to use compiler directives to handle the different events on each
side.
#if SILVERLIGHT
#else
#endif
Fele replied on Friday, October 23, 2009
Thanks for response.
If I correctly understood your last post, I should handle different events on client and server.
From my testing, I figured out that I can use OnChildChanged on server. I'm not sure which event I should handle on client.
If you can provide short code example, I will appreciate.
Thanks
Fele
RockfordLhotka replied on Friday, October 23, 2009
You can override OnChildChanged() on client and server. What I
am saying is that inside that override you’ll need to treat the ‘e’ parameter
differently because on the server it will contain the ListChanged event args
and on the client it will contain the CollectionChanged event args:
protected override void OnChildChanged(…)
{
#if SILVERLIGHT
// use CollectionChanged event args
#else
// use ListChanged event args
#endif
}
Copyright (c) Marimer LLC