Update parent property when child property changes

Update parent property when child property changes

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


Jeroen Sneyers posted on Thursday, December 21, 2006

Hi all, I'm stuck with a small problem here and I hope one of you can help me out.

I have an EditableRoot object 'Offer', an EditableChildList object 'OfferLines' and an EditableChild object 'OfferLine'. (Basically used as a document with header info and detailed line info). OfferLine contains a property 'Amount' and Offer contains a property 'Total'. Now, I want that my 'Total' (of the offer) is being updated (using databinding) when the property 'Amount' of one of the offerlines changes. I use databinding in a windows form. How do I do this?

Jeroen.

xal replied on Thursday, December 21, 2006

You need to handle PropertyChanged for each of the children inside the root. When you get that event, check to see what property changed in the child and if necessary update the values you need in the root and raise PropertyHasChanged("<Name of the property in the parent>").
Be careful about this when you use remoting, you may need to remove the handlers and readd them when the object is serialized / deserialized. (I don't remember if that event is serializable)

Andrés

ajj3085 replied on Thursday, December 21, 2006

I'm pretty sure its not, as I've lost events.  You don't even need remoting to lose the events either, if you follow the WinForms pattern of saving a clone of the business object, because the cloning does serialization / deserialization as well.

BVeenstra replied on Thursday, September 27, 2007

ajj,

Hopefully better late than never...

You might want to review this thread: http://forums.lhotka.net/forums/thread/17490.aspx.  This helped me resolve this finally...



I had a similar setup where I was losing "events" when Saving and also trying to have a "parent" object update itself when it's child list changed... 

That's a two step process, #1, you have to be sure setup a handler and hook:

            //...

        private AllocationDetailList _AllocationDetailList = null;

            //...


        private void Fetch(SafeDataReader dr)

        {

 

            //...


            _AllocationDetailList.ListChanged += new ListChangedEventHandler(AllocationDetailList_ListChanged);

 

            MarkOld();

 

        }


            //...


        void AllocationDetailList_ListChanged(object sender, ListChangedEventArgs e)

        {

            PropertyHasChanged("RemainingAmount");

        }


            //...





Step #2 - you need to rehook your List's ListChanged in your OnDeserialized method:

        protected override void OnDeserialized(System.Runtime.Serialization.StreamingContext context)

        {

            base.OnDeserialized(context);

            //REQUIED to rehook us up to DataBinding after SAVE!

            _AllocationDetailList.ListChanged += new ListChangedEventHandler(AllocationDetailList_ListChanged);

        }








Copyright (c) Marimer LLC