Search code examples
c#wpfvisual-studio-2010workflow-foundation-4

Retrieve activity name


I'm working in a WPF project and I need one requirement. This is my main window:

http://img210.imageshack.us/img210/9752/rule.jpg

So, I want that when I drag and drop a custom activity (aggregate Functions), display his name in Compute TextBox... Is there some way to do it????

For example:

1) Drag and drop MaxActivity

2) Display in TextBox Compute: MaxActivity

3) Drag and drop SumActivity

4) Display in TextBox Compute: MaxActivity + SumActivity

5) Drag and Drop MaxActivity

6) Display in TextBox Compute: MaxActivity + SumActivity + MaxActivity1

Thanks a lot!


Solution

    1. I would strongly suggest you avoid this design, as however you end up implementing it will introduce brittleness to your code without any benefit (I can already see you are aggregating max and sums, why tell me again?)
    2. If you go ahead, get rid of that "Compute" TextBox. TextBoxes = enter text. You are just rephrasing what is already in the design surface. It makes no sense to allow me to change the text you generate. Use a TextBlock or Label.
    3. Do not, I repeat, not save this in your Activity. This is all UI tasks and should not be saving this information in your Activity configuration

    If you have this

    public sealed class MyActivity : Activity
    {
        public string Compute {get;set;} // NO!
    

    and this

    <!-- NO! -->
    <Label>Compute</Label><TextBox Text="{Binding ModelItem.Compute}"/>
    

    you're probably doing this wrong.

    So, how do you do it?

    You can parse out your ModelItem and generate this string by listening to changes in the ModelItem and constructing this string every time. Note, your ModelItem will not be set when the constructor is called, so you are going to have to listen to changes to the ActivityDesigner.ModelItem property. There is no event for this, so you will have to know how to listen to changes in a DependencyProperty.

    Once you are listening to changes in your ModelItem, whenever a change happens, you can walk down your ModelItem (which is hard) or just get the Activity from the ModelItem and examine it.

    var child = ModelItem.Properties["Child"].Value.GetCurrentValue();
    if(child == null)
        computeTextBox.Text = "Please add some computation stuff.";
    else
        // hard stuff goes here...
    

    Walking down the path from your Activity's child to whatever activities are held inside can be treacherous, as you can't assume your user has thrown in a Sequence rather than a single MaxActivity, or that they don't have seven nested Sequences, etc etc. So you have to have a pretty complex and brittle set of if else logic to try and parse out what is contained below. And if you start changing properties of the Activity tree outside of the ModelItem you're going to get out of sync with the UI.

    Now, after reading that, go back to point 1. I listed in this answer and follow my advice. Drop the idea completely.