Using the StyleOn Style Property in Dynamic Resources

I wonder if there is a way to use the basedOn property of wpf styles with dynamic resources. eg.

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

this, for example, produces an error indicating that using dynamic resources in combination with BasedOn styles is not possible. I wonder how someone could do this? thank

+22
wpf xaml
Feb 25 '09 at 10:10
source share
2 answers

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

  Style A / \ Style A1 Style A2 

this may not be a complicated scenario. You reference StyleA using a dynamic resource, so when this resource changes, Style A1 and Style A2 must change their BasedOn property. However, as soon as the style is used in your application, it becomes a sealed object. Style A becomes unchanged.

One workaround you can use is:

  • Style A needs to be changed.
  • Create a new Style object that will become the new Style A resource.
  • Create a new version of Style A1 and Style A2 . You will need to write a copy procedure that will make copies of all Setters , Resources , etc. Install BasedOn in the new version of Style A
  • Update your resource collection to have three new styles.

{DynamicResource StyleA1} and {DynamicResource StyleA2} should now understand that these resources change (from step 4) and automatically update any links.

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

Hope I understood your problem and helped.

+15
Feb 25 '09 at 14:37
source share

I found that since you cannot use BasedOn in DynamicResource , you can "convert" DynamicResource to StaticResource by combining the ResourceDictionary with your "parent" resources to your current window / UserControl / whatever. Thus, now you can reference a resource object (e.g. Style ) using a StaticResource . This way you can use Datatriggers in DynamicResource (via conversion).

Example:

 <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/MyProject.Styles;component/ButtonStyles.xaml"/> </ResourceDictionary.MergedDictionaries> [*Your other resources can be put here*] </ResourceDictionary> </Window.Resources> ... <Button Command="{Binding MyCommandInViewModel, RelativeSource={RelativeSource AncestorType=Window}}"> <Button.Style> <Style BasedOn="{StaticResource StyleFromButtonStyles}" TargetType="Button"> <Style.Triggers> <DataTrigger Binding="{Binding SomeBool}" Value="True"> <Setter Property="Button.Content" Value="{StaticResource SomeImage}"/> </DataTrigger> <DataTrigger Binding="{Binding SomeBool}" Value="False"> <Setter Property="Button.Content" Value="{StaticResource SomeOtherImage}"/> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button> 

Therefore, Datatriggers is applied to the button created in the imported ResourceDictionary .

Hope this helps!

+11
Feb 17 '12 at 10:19
source share



All Articles