WPF: Applying Inherited Styles to Inherited Elements


The CustomToggleButton element inherits from the standard ToggleButton :

class CustomToggleButton : ToggleButton {

    static CustomToggleButton() {
            typeof(CustomToggleButton), new FrameworkPropertyMetadata(typeof(CustomToggleButton)));

    // ...   

ToggleButtonCommon is the main style settings for the CustomToggleButton , and InGroupToggleButton is the style inherited from it:

<Style  x:Key="ToggleButtonCommon"
        TargetType="{x:Type local:CustomToggleButton}" 
        BasedOn="{StaticResource {x:Type ToggleButton}}">

        // ...

<Style x:Key="InGroupToggleButton" BasedOn="{StaticResource ToggleButtonCommon}">
    <Setter Property="Control.Margin" Value="5 5 5 0" />

It turned out that only ToggleButtonCommon can be applied to the CustomToggleButton element, but not InGroupToggleButton or any other derived style.

What is the reason for this phenomenon and is it possible to apply inherited styles to the CustomToggleButton ?


Add TargetType="{x:Type local:CustomToggleButton}" style definition.

Scroll to Top