locked
ModelItem.View returns null for FlowStep model items RRS feed

  • Question

  • Hi,

    I was continuing on with the code first discussed in an earlier post about how to get a child model item from a flow chart model item.  For a given child model item, I discovered that if the model item wraps a FlowStep, then attempting to access the View property of the model item returns null.  If the model item instead wrapped a FlowDecision activity, then the View property returned a non-null value.


    For example, in the code below:

                    ModelItemCollection nodeModelItems = _activeflowChartDesigner.ModelItem.Properties["Nodes"].Value as ModelItemCollection;
                    foreach (ModelItem modelItem in nodeModelItems)
                    {
                        WorkflowViewElement view = modelItem.View as WorkflowViewElement;
                        if (view == null)
                        {
                            view = _wd.Context.Services.GetService<ViewService>().GetView(modelItem) as WorkflowViewElement;
                        }
                        if (view != null)
                        {
                            //Rect activityRectangle = new Rect(view.PointToScreen(new Point(0, 0)), view.PointToScreen(new Point(view.ActualWidth, view.ActualHeight)));
                        }
                    }


    modelItem.View returns null (even before trying to cast it as a WorkflowViewElement).  I then tried getting the view from the ViewService (as shown above), and that sort of worked, but the view wasn't property initialized; for example, the commennted out call of PointToScreen fails.

    The above code fragment is not called when a model item is just added to the designer, but much later, so I expect the View should have ample time to create itself.

    How can I get the View from a ModelItem wrapping a FlowStep?

    Thanks,

    Notre
    Tuesday, February 9, 2010 10:53 PM

Answers

  • confused. I create a flowchart, and put a child in it wrapped by flowstep

    then I use following code to get the view correctly

    item.Properties["Nodes"].Collection[0].Properties["Action"].Value.View

    note: this time the child has displayed on flowchart designer

    I'm really a little confused.
    would you mind pasting your code here so I can find out the problem?
    • Marked as answer by Notre Thursday, February 11, 2010 12:14 AM
    Wednesday, February 10, 2010 12:19 AM
  • For the benefit of anyone else reading this, the problem I was experiencing was that the FlowStep wraps most activities, except the FlowDecision and FlowSwitch.  As Ye mentions, the FlowStep itself doesn't have a view, so the View property on the ModelItem returns null.  If instead I try to use the ViewService, this creates a view of sorts, but it is not actually part of the visual tree, so lots of stuff doesn't work.

    The trick then is to detect if the ModelItem is a FlowStep, get the contained activity as a ModelItem, and then get the view from there.  The following code worked for me (thanks to Ye for the general idea):

                    WorkflowViewElement view;
                    ModelItem action = null;
                    if (modelItem.ItemType == typeof(FlowStep))
                    {
                        action = modelItem.Properties["Action"].Value;
                        view = action.View as WorkflowViewElement;
                    }
                    else
                        view = modelItem.View as WorkflowViewElement;

    Notre

    • Marked as answer by Notre Thursday, February 11, 2010 12:14 AM
    Thursday, February 11, 2010 12:12 AM

All replies

  • So in fact flowstep doesn't have view. so the modelitem for flowstep has no view. this is why the view property is null.
    secondly, if you use ViewService to getView, if the modelitem has no view, then it will create the default view.
    so in fact, for your code, it's not recommended to use getView. the Default View is not something you need, I guess.
    Tuesday, February 9, 2010 11:01 PM
  • Ok, if a FlowStep doesn't have a view, how about the Activity that it wraps?  I'd like to get its View somehow.  The wrapped activity (the one inside the FlowStep) is visually on the designer, so it must have a view....

    Thanks,

    Notre
    Tuesday, February 9, 2010 11:06 PM
  • are you talking about the link in the flowchart?

    it's flowlink.
    it's not something public as I know.

    Tuesday, February 9, 2010 11:24 PM
  • No, I'm thinking of, for example, a Sequence activity (or any other activity, other than FlowDecision or FlowSwitch).  When it gets dropped onto the FlowChart designer, the Sequence activity gets wrapped in a FlowStep.  There's still a visual for the Sequence on the FlowChart.  But when I access the model item, I get a FlowStep.  I want to get the View for the Sequence that I see dropped on the FlowChart.

    Notre
    Tuesday, February 9, 2010 11:31 PM
  • so in fact you want to get the view of the dropped sequence such as sequence designer, right?


    Tuesday, February 9, 2010 11:41 PM
  • Yes, that's right.
    Tuesday, February 9, 2010 11:45 PM
  • ok. that's easy.
    do you remember long long ago I pasted you a answer about how to get the view.
    now we can get the activity modelitem wrapped in flowstep, right?
    something like this, root.Properties["Nodes"].Collection[ind].Properties["Action"].ComputedValue

    so if you want to get the view of the activity when first time you drop the activity to the flowchartdesigner
    you need to use the viewservice to get the designer view, because this time, the ModelItem.View hasn't been created.
    if you want to get the view of the activity after the designer has been displayed on the flowchart designer, you need to use ModelItem.View to get the activityDesigner.

    is this what you want?
    Hope I'm not misunderstanding.
    Tuesday, February 9, 2010 11:50 PM
  • Right - so if the model item is just being added, I understand that the View is not really prepared yet, so we use the ViewService to get the view.  But, in this case, the view should be created! I'm not doing this logic from e.g. model service modelChanged event (where model items are being added).  I'm interacting with the designer well after this point. 

    And my observation is that if you a non-wrapped activity, like FlowDecision, then when I ask for the View, I get it right away.  With a FlowStep, in the same scenario, the View property is null.  When I ask for the View by using the ViewService on the FlowStep, I do get something back.  However, it is not properly initialized (at least not yet), and so when I try to do things like call PointToScreen or test the Width property, these either through an exception or a NaN.  Contrast this with the FlowDecision, where both of these scenarios work fine.
    • Edited by Notre Tuesday, February 9, 2010 11:57 PM add "not" really
    Tuesday, February 9, 2010 11:57 PM
  • confused. I create a flowchart, and put a child in it wrapped by flowstep

    then I use following code to get the view correctly

    item.Properties["Nodes"].Collection[0].Properties["Action"].Value.View

    note: this time the child has displayed on flowchart designer

    I'm really a little confused.
    would you mind pasting your code here so I can find out the problem?
    • Marked as answer by Notre Thursday, February 11, 2010 12:14 AM
    Wednesday, February 10, 2010 12:19 AM
  • Hi Ye,

    My code (an abbreviated version, anyway), is pasted above.  The full code is quite a bit longer.  I can email it to you, if you like.

    Note that I'm using Beta 2 at the moment; my other machine is still installing the RC.  I'll try it with the RC, once it's finished.

    Notre
    Wednesday, February 10, 2010 12:50 AM
  • maybe you need to have a try on RC. I tested this on my RC build, it works fine.

    by the way, what you want to do with the activity designer? relocate the child in the flowchart designer?
    Wednesday, February 10, 2010 1:02 AM
  • Yes, I will try on RC.

    I'm implementing rubber band / rectangle selection support; when I draw a rectangle over top of the window, I want to know what elements (activities) are within that rectangle, then I will select them.
    Wednesday, February 10, 2010 1:06 AM
  • Just tried it on RC; still not working for me.

    Notre
    Wednesday, February 10, 2010 1:59 AM
  • strange.
    would you mind send a simple sample code to me? for I use your above code to get pointToScreen it works.

    my mail: soyu at microsoft.com
    Wednesday, February 10, 2010 2:02 AM
  • Ok, I have just emailed a simple example to the email address you provided. It will be from:

       notre_poubelle at yahoo.com

    Thank you,

    Notre
    Wednesday, February 10, 2010 9:05 PM
  • I have mailed you the code and explaination.
    Wednesday, February 10, 2010 11:16 PM
  • For the benefit of anyone else reading this, the problem I was experiencing was that the FlowStep wraps most activities, except the FlowDecision and FlowSwitch.  As Ye mentions, the FlowStep itself doesn't have a view, so the View property on the ModelItem returns null.  If instead I try to use the ViewService, this creates a view of sorts, but it is not actually part of the visual tree, so lots of stuff doesn't work.

    The trick then is to detect if the ModelItem is a FlowStep, get the contained activity as a ModelItem, and then get the view from there.  The following code worked for me (thanks to Ye for the general idea):

                    WorkflowViewElement view;
                    ModelItem action = null;
                    if (modelItem.ItemType == typeof(FlowStep))
                    {
                        action = modelItem.Properties["Action"].Value;
                        view = action.View as WorkflowViewElement;
                    }
                    else
                        view = modelItem.View as WorkflowViewElement;

    Notre

    • Marked as answer by Notre Thursday, February 11, 2010 12:14 AM
    Thursday, February 11, 2010 12:12 AM