tags:

views:

1305

answers:

1

hi there,

i wonder if there is a way to use the basedOn property of wpf styles with dynamicresources. e.g.

<Style BasedOn="{DynamicResource somestyle}">
   <Setter Property="SomeProp" Value="SomeValue"/>
</Style>

this e.g. throws an error indicating that the usage of dynamicresources in combination with BasedOn styles is not possible. i wonder how someone could do that? thanks

+2  A: 

I think the main reason is sealed objects. If you have a Style hierarchy:

       Style A
      /       \
  Style A1  Style A2

this might not be a difficult scenario. You refer to StyleA using a dynamic resource, so whenever that resource changes, Style A1 and Style A2 should change their BasedOn property. However, once a Style is being used in your application, it becomes a sealed object. Style A becomes immutable.

One workaround you can use is:

  1. Style A needs to change.
  2. Create a new Style object that is going to be the new Style A resource.
  3. Create a new version of Style A1 and Style A2. You'd need to write a copy procedure that makes copies of all the Setters, Resources, etc. Set the BasedOn to the new version of Style A.
  4. Update the resources collection so that the three new styles are in there.

{DynamicResource StyleA1} and {DynamicResource StyleA2} should now pick up the fact that those resources changes (from step 4) and update any references automatically.

Note that this is a very simple scenario. Real world style hierarchies can be more complex, especially if they are spread across multiple files and come from merged dictionaries.

Hope I understood your problem and helped out.

siz