locked
How to walk down the model item tree? RRS feed

  • Question

  • Hi,

    I know how to walk up the model item tree, form a given model item.  But how do I walk down the tree, from a given model item (i.e. what properties would I use to get the immediate children of a given model item)?

    Thanks,

    Notre
    Wednesday, February 3, 2010 12:29 AM

Answers

  • Notre,  you can use following code

    //assume all the activites you created are on the designer surface.  use item.view to check whether the modelitem is an activity. if view != null, it's an activity.
    if(CurrentModelItem.View != null)
    {
          //get all properties as Modelitem
         //foreach all properties, call get children from properties
    }
    else
    {
        //call get children directly
    }

    private list<ModelItem> Getchildren(ModelItem currentModelItem)
    {
    ModelItemCollection collection = CurrentModelItem as ModelItemCollection;
    if(collection != null)
    {
        //go through the whole collection to get all children
    }
    else
    {
        ModelItemdictionary dictionary = CurrentModelItem as ModelItemDictionary;
        //go through the whole dictionary to get all children
    }
    }

    Hope it helpful.

    • Edited by Ye Yu - MSFT Wednesday, February 3, 2010 7:03 AM
    • Marked as answer by Notre Wednesday, February 3, 2010 5:39 PM
    Wednesday, February 3, 2010 5:41 AM

All replies

  • Hi, do you mean to get child activity of a given model item? If so, you can do it like this:

    Type

     

     

    type = rootItem.ItemType;

     

     

    if (type == typeof(Sequence))

    {

     

     

    ModelItemCollection mc = rootItem.Properties["Activities"].Collection;

     

     

    ModelItem firstChild = mc[0];

    ......

    }

     

     

    else if (type == typeof(While))

    {

     

     

    ModelItem bodyItem = rootItem.Properties["Body"].Value;

    }
    else if ...
    ...

    Wednesday, February 3, 2010 5:18 AM
  • Notre,  you can use following code

    //assume all the activites you created are on the designer surface.  use item.view to check whether the modelitem is an activity. if view != null, it's an activity.
    if(CurrentModelItem.View != null)
    {
          //get all properties as Modelitem
         //foreach all properties, call get children from properties
    }
    else
    {
        //call get children directly
    }

    private list<ModelItem> Getchildren(ModelItem currentModelItem)
    {
    ModelItemCollection collection = CurrentModelItem as ModelItemCollection;
    if(collection != null)
    {
        //go through the whole collection to get all children
    }
    else
    {
        ModelItemdictionary dictionary = CurrentModelItem as ModelItemDictionary;
        //go through the whole dictionary to get all children
    }
    }

    Hope it helpful.

    • Edited by Ye Yu - MSFT Wednesday, February 3, 2010 7:03 AM
    • Marked as answer by Notre Wednesday, February 3, 2010 5:39 PM
    Wednesday, February 3, 2010 5:41 AM
  • another way to get the children ModelService.Find(ModelItem startingItem, Predicate<Type> match) and you need to difine the predicate<Type> match yourself to get the children you need.
    Wednesday, February 3, 2010 7:02 AM