Changing Dependency Properties from Code

Hi guys,
as I'm getting always the right answers here - I have another "simple" one. When I change a dependency property of an activity in code the WorkflowDesigner is not persisting it because it does not "realise" that the prop has changed.
No matter if I do it directly like activityinstance.Property = "string"; or through activityinstance.SetValue(Activity.PropertyProperty, "string");
This is different if I change the settings through the UI using the Properties pane. Question is - what function or property will cause the WorkflowDesigner to persist the workflow definitions



Answer this question

Changing Dependency Properties from Code

  • Chuck B

    Hi Edmundas,

    Are you using your activity in a StateMachineWorkflow, WhileActivity, ReplicatorActivity, or ConditionedActivityGroup (I'm guessing it's a state machine) If so, then you're probably running into an ActivityExecutionContext issue. Please read my post below if this is the case and it should help you through it:

    http://forums.microsoft.com/MSDN/showpost.aspx postid=332207&siteid=1

    Thanks,
    Angel



  • Twindagger

    Hello all,

    I have similar problem:

    (1) If I set activity dependency property in the overriden Execute method of the activity class, those changes are not reflected when workflow is executed.

    (2) If I set dependency property i.e. in the overriden Initialize method of the activity class, it works fine, updated value is used in the workflow.

    Workaround for the (1) situation - I bind dependency property value to some member variable and access that member variable instead of activity property and it works fine.

    Edmundas

    Tested activity code:

    public class TestActivity : System.Workflow.ComponentModel.Activity
    {
    public static DependencyProperty TestProperty = DependencyProperty.Register("Test", typeof(string), typeof(TestActivity));

    [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
    [ValidationOption(ValidationOption.Optional)]
    [BrowsableAttribute(true)]
    public string Test
    {
    get
    {
    return (string)(base.GetValue(TestActivity.TestProperty));
    }
    set
    {
    base.SetValue(TestActivity.TestProperty, value);
    }
    }

    public TestActivity()
    {
    }

    protected override void Initialize(IServiceProvider provider)
    {
    base.Initialize(provider);
    //this.Test = "TestString"; // If property value is set here, it works fine
    }

    protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
    {
    this.Test = "TestString"; // If property value is set here, it is forgotten
    return base.Execute(executionContext);
    }
    }


  • asqui

    If you change/add code in the InitializeComponent() method (granted it must be correct), then the designer will recognize that as well.

    When you configure an activity at design time through the Property Browser in the designer, VS will emit code into the InitializeComponent() method in the .Designer.cs file that corresponds to your configuration (code serialization). Everytime you open up the designer associated with an activity/workflow, the VS will parse the InitializeComponent() method in the .Designer.cs file and create a visual display of the workflow (code deserialization).

    So if you add some code to the InitializeComponent() method to configure an activity, it will also be reflected in the Property Browser when you open the designer afterwards.

    Hope this helps,
    Angel



  • Ice2Fire

    Hi Mario,

    The designer essentially "deserializes" what it finds in the InitializeComponent() method that it generates in the .Designer.cs file. If you're setting it anywhere else, it does not recognize it.

    Thanks,
    Angel



  • Futzy

    Angel,

    This thread was obviously dedicated to design time issues. We have come upon a runtime problem with persisting a DependecyProperty.

    We are working with StateMachine workflows and we have one workflow invoking another by use of an activity. In the Execute method of the activity, we start the other workflow and store the Guid in a DependencyProperty. The PersistenceService does its thing, but when the workflow is activated again, that DependencyProperty is null and the Guid we assigned it wasn't persisted.

    Can you help regarding this issue Thanks.

    Mike

  • chinkai

    but VS can set it at Design timem and it gets recognized. How does it do it

    Mario


  • Flippy_TK

    Hope this question is not too silly - so someone can answer. Please help! When I try to change a DependencyProperty from code in design mode it does not work. Maybe someon can provide an example.

    Kind Regards
    Mario


  • Changing Dependency Properties from Code