Using BasedOn Style Property on DynamicResources

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

override style through basedon style property

I have this base style for a button: <Style x:Key=ButtonStyle_base TargetType={x:Type Button}> <Setter Property=Template> <Setter.Value> <ControlTemplate TargetType={x:Type

How to change settings of a base style from derived settings using BasedOn property

I have simple style with ControlTemplate OrangeButton, and I have created another style OrangeButton2 BasedOn OrangeButton. I am trying to change the border properties, but WPF doesn’t render the new

How to apply WPF styles based on top of another style without using BasedOn property

I am styling CellValuePresenter (From Infragistics) to give different look to Gid Lines and have defined a style (gridLineStyle) and applied to the Grid’s CellValuePresenterStyle Property. I have dis

Using BasedOn property with a Style defined in a different dictionary

The application I’m working on has 2 ResourceDictionary, DefaultStyles.xaml and CustomStyles.xaml. Is it possible that a style in the CustomStyles dictionary uses a base style defined in the other dic

Style BasedOn a user-defined style

I have a DefaultStyle and then another Style defined like this <Style TargetType={x:Type my:CustomButton} BasedOn={StaticResource DefaultStyle} x:Key=DefaultCustomButton> … Then, having

BasedOn property not working with ListView

When I use the following code it works because I am using a ListBox <UserControl.Resources> <Style BasedOn={StaticResource {x:Type ListBox}} TargetType={x:Type ListBox}> <Setter Pro

How to properly set the BasedOn in XAML style

I have this as the style template: <Style x:Key=myDogToggleButton1 TargetType=ToggleButton BasedOn={x:Null}> <Setter Property=FocusVisualStyle Value={x:Null}/> <Setter Propert

WPF BasedOn Style with Style.Resources

Hi there, I’m asking for your help, as I can’t get my ‘BasedOn’ style to work properly. Here is the situation : I have my base style defined in a ResourceDictionary, like this : Styles.xaml <Styl

Why do folks specify BasedOn=“{x:Null}” in a XAML Style element?

I see XAML markup like this: <Style x:Key=SmallSquareButton BasedOn={x:Null} TargetType={x:Type Button}> And I wonder why it is necessary to set the BasedOn property to null. From the doc

WPF style basedon current

Is there a way to create a style that extends the current style, i.e. not a specific style? I have a WPF application where I create styles to set some properties like borders or validation. <Style

Answers

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.

I’ve found that since you can’t use BasedOn on a DynamicResource, you can “convert” the DynamicResource to StaticResource by merging the ResourceDictionary holding your “parent” resources to your current Window/UserControl/whatever. This way you are now able to refer to the resource object (eg. Style) using StaticResource. This way you can use Datatriggers on DynamicResource (through 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>

Hence Datatriggers are applied to a button styled in an imported ResourceDictionary.

Hope this helps!