How to catch a newly added Item to a list.

How to catch a newly added Item to a list.

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


pgroenenstein posted on Saturday, December 11, 2010

Hi

Background: Developing a Silverlight App with CSLA 3.8.4

I have an "Order" object (Inherits from BusinessBase) , which contains a List Object named "OrderItems" (Inherits from BusinessListBase)  with it's childs named "OrderItem" (Inherits from BusinessBase).

What I need to know is how would one "Catch" a newly added child ("OrderItem) to the list object ("OrderItems")  in the Parent object  ("Order") as well as how to catch that in the UI (an SL 3 App)?

Another part of the problem is, how does one see in the "parent" ("Order") object that a child("OrderItem") was removed in the list ("OrderItems") object  I need to know in the parent when the deleting of a child was completed successful, in order to do sum stuff in the parent object.

Thanks

Phillip

 

 

 

JonnyBee replied on Saturday, December 11, 2010

The OrderItems list has an event called ListChanged and the Event parameter gives you the reason (added/removed/changed) and the index of the item in the list. You can hook in here to get a newly added item. 

The Order object also has a ChildChanged event that you should look into. I'd place the sum stuff into this event. I can't remember right now if this event will also give you the objecttype/property and reason but look into it and keep us updated.

 

 

pgroenenstein replied on Saturday, December 11, 2010

Thanks Jonny for your help.

Here is my solution for others that might want to know the same thing.

This code resides in my root object Named "Order", because the ChangeEvents cascades all the way up to the Root object (IAs long as your child objects are in a collection, or are referenced through a managed backing field )

The important thing to remember here is that the code in your Client Business Object  would need to be different between a Silverlight App and a  "normal" dotNet Appt, hence the Complier Directive for Silverlight. (Of course if you make use of partial classes you won't need the directive, I personally prefer compiler directives.)

The "first" part of the sub can be the same for both Silverlight and the .Net Client.The difference is in the "second" part since the Silverlight Client do not have access to the " ListChangedEventArgs" but rather the "CollectionChangedEventArgs"

The difference is how the Case Block (right under the comment "Handle List Object changes here.") is structured in the second part,

#If SILVERLIGHT Then
    ' Silverlight only.
    Protected Overrides Sub OnChildChanged(ByVal e As ChildChangedEventArgs)

      ' Re-Raise to UI
      MyBase.OnChildChanged(e)

      If e.PropertyChangedArgs IsNot Nothing Then
        ' a Poperty has changed
        Select Case e.PropertyChangedArgs.PropertyName
          Case "LineTotal"
            ' a Propery named "LineTotal" on a Child of the List Object has changed, if need be do some stuff here on the Root Object.
            RecalcTotals()
        End Select

      Else

        ' Handle List Object changes here.
        If e.ChildObject.ToString() = "SmtSelfOrder.Business.BusinessClasses.OrderItems" Then
          'Check Type of Action on list
          Select Case e.ListChangedArgs.Action
            Case Collections.Specialized.NotifyCollectionChangedAction.Add
              ' Do stuff here you want to do when Adding a Child to the List object.
              RecalcTotals()
            Case Collections.Specialized.NotifyCollectionChangedAction.Remove
              ' Do stuff here you want to do when Deleting a Child from the List object.
              RecalcTotals()
          End Select
        End If
      End If

    End Sub

#Else
    ' .Net Only
    Protected Overrides Sub OnChildChanged(ByVal e As ChildChangedEventArgs)

      ' Re-Raise to UI
      MyBase.OnChildChanged(e)

      If e.PropertyChangedArgs IsNot Nothing Then
        ' a Poperty has changed
        Select Case e.PropertyChangedArgs.PropertyName
          Case "LineTotal"
            ' a Propery named "LineTotal" on a Child of the List Object has changed, if need be do some stuff here on the Root Object.
            RecalcTotals()
        End Select

      Else

        ' Handle List Object changes here.
        If e.ChildObject.ToString() = "SmtSelfOrder.Business.BusinessClasses.OrderItems" Then
          'Check Type of Action on list
          Select Case e.ListChangedArgs.ListChangedType
            Case ListChangedType.ItemAdded
              ' Do stuff here you want to do when on Adding a Child to the List object.
              RecalcTotals()
            Case ListChangedType.ItemDeleted
              ' Do stuff here you want to do when Deleting a Child from the List object.
              RecalcTotals()
          End Select
        End If
      End If

    End Sub

#End If

 

Copyright (c) Marimer LLC