This will be my first post on Silverlight!
The difference between the two can be summarized as:
- StaticResource is evaluated at the start of the application, and will find resources defined before its actual usage;
- DynamicResource is evaluated at a later point, and will find resources declared anywhere, before or after its usage.
Unfortunately, Silverlight does not include DynamicResource, but it is relatively easy to achieve something like that; that is the purpose of this post.
Check out the following code:
I won’t go into details on markup extensions and all that, but basically, this one hooks to the Loaded event of the FrameworkElement, thus deferring the setting of the property. The property and the target are found through the IProvideValueTarget instance and the resource is looked up recursively from the target element up to the Application instance. If the property to be set, or its class, defines a type converter, it will try to convert the found resource to the proper type. In the meantime, it just returns the default value of the property type, null or a default instance, in case of a value type.