28

I have a theme that is applied to all buttons in a resource dictionary. Now I want to add a trigger to the button while inheriting the style changes from the dictionary. I tried the following code, but it says that the control cannot be found. How can I fix it ?

<UserControl.Resources>
  <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="Theme.xaml"/>
      </ResourceDictionary.MergedDictionaries>

      <conv:ErrorContentConverter x:Key="ErrorContentConverter" />

      <Style x:Key="ValidTrigger" 
             TargetType="Control" BasedOn="{StaticResource {x:Type Control}}">
         <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsValid}" Value="False">
              <Setter Property="IsEnabled" Value="false" />
            </DataTrigger>
         </Style.Triggers>
      </Style>   
  </ResourceDictionary>
</UserControl.Resources>

The base template:

    <Style TargetType="{x:Type Button}" BasedOn="{x:Null}">
    <Setter Property="FocusVisualStyle" 
            Value="{DynamicResource NuclearButtonFocusVisual}" />
    <Setter Property="Foreground" Value="#FF042271" />
    <Setter Property="FontFamily" Value="Trebuchet MS" />
    <Setter Property="FontSize" Value="12" />
    <Setter Property="Padding" Value="3" />

    <Setter Property="Template" Value="{DynamicResource ButtonTemplate}" />
</Style>

3 Answers 3

36

One trick I've used in the past: in your ResourceDictionary that defines blanket styles for your application, add an x:Key to the style you'd like to inherit from, like so:

<Style TargetType="{x:Type Button}" x:Key="ButtonStyle">
  <!-- your style here -->
</Style>

To apply this style to all controls of the specified type (Button in this example) without having to explicitly set the Style attribute of every Button, add another style that's BasedOn this style, but doesn't have a key:

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonStyle}" />

Using this method, all Buttons in your application will automatically inherit your custom style, but you can still create additional styles BasedOn the ButtonStyle entry and avoid blowing away all of your custom styling.

33

Give your base Style a name, say FooStyle.

In the example you gave, modify the TargetType and BasedOn to look as follows:

 <Style x:Key="ValidTrigger" 
        TargetType="{x:Type Control}" BasedOn="{StaticResource {x:Type Control}}" >
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsValid}" Value="False">
            <Setter Property="IsEnabled" Value="false" />
        </DataTrigger>
    </Style.Triggers>
</Style>
4
  • Can I do it without assigning a name? There is only one style for the control in the dictionary.
    – Marcom
    Commented Apr 15, 2011 at 10:03
  • Do you mean that, rather than assign a key to the base style, you would like the extended style to figure out which style to extend based on it's TargetType? I don't know. Commented Apr 15, 2011 at 10:21
  • Why do you hesitate to give the base style an x:Key="FooStyle" ? Commented Apr 15, 2011 at 10:26
  • 3
    @PieterMüller: If you assign a key to the style, the style is no longer used by default by all matching controls within the context. Nathan Friend's answer provides a solution for this. I just got the same problem :) Commented May 2, 2018 at 13:07
3

I think there is no base style defined for "control" so your BasedOn="{StaticResource {x:Type Control}}" part won't find anything.

You probably want to change

<Style x:Key="ValidTrigger" TargetType="Control" BasedOn="{StaticResource {x:Type Control}}" >

to

<Style x:Key="ValidTrigger" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}" >

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.