WPF: Applying Inherited Styles to Inherited Elements

Question:

The CustomToggleButton element inherits from the standard ToggleButton :

class CustomToggleButton : ToggleButton {

    static CustomToggleButton() {
        CustomToggleButton.DefaultStyleKeyProperty.OverrideMetadata(
            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>

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

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 ?

Answer:

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

Scroll to Top
AllEscort