if you've ever implemented design time support for a component or control in .NET, then you're probably familiar with the [DefaultValue] attribute, which allows the component developer to tell the designer what the default value for a given property is so that the "Reset" option in the property grid is enabled and knows to what default value to "reset" the property to when invoked.

You also probably know that it's your responsability to initialize the property to the default value on your component's constructor so that it matches what you specified in the [DefaultValue] attribute, otherwise the user will get somewhat unexpected results.

When working with Windows Workflow Foundation (and WPF, I imagine), you can certainly use the [DefaultValue] attribute in your custom activity properties, and it works just like in Windows Forms or ASP.NET. However, in WF you'll use Dependency Properties a lot, and you might notice that when you register a dependency property you can also specify a default value through the PropertyMetadata parameter.

I initially thought they would just do the same thing. I was wrong, however, and they are not the same:

  • When you specify a default value through PropertyMetadata, that value is used to physically initiallze the dependency property, so there's no need for you to actually initialize the property to the correct value in the activity's constructor.
  • The default value specified through the PropertyMetadata is not taken into account by the design-time infrastructure, unlike the [DefaultValue] attribute.

So, it seems that, again, both default value options are actually complementary and you should use both to get the full effect. That said, this gives me little reason to specify default values through the PropertyMetadata instance, when initializing the property in the constructor is just clearer (to me at least).

Thanks to Patrick in the WF forums for getting me look at this closer!

Tomas Restrepo

Software developer located in Colombia.