meppl
meppl

Reputation: 73

WPF Style BasedOn not working as expected

Why do my Radiobutton not look like a togglebutton? Take a look at the code

<Style x:Key="ButtonBaseStyle" TargetType="{x:Type ButtonBase}">
<Setter Property="Height" Value="100" />
</Style>
<Style BasedOn="{StaticResource ButtonBaseStyle}" TargetType="{x:Type Button}" />
<Style BasedOn="{StaticResource ButtonBaseStyle}" TargetType="{x:Type ToggleButton}" />
<Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="{x:Type RadioButton}" />


<StackPanel>
    <Button>Button</Button>
    <ToggleButton>Toggle</ToggleButton>
    <RadioButton>Radio</RadioButton>
</StackPanel>

If I remove the buttonbase style it works

Upvotes: 1

Views: 1374

Answers (1)

Kylo Ren
Kylo Ren

Reputation: 8813

The looks of your RadioButton is not as ToggleButton cause of you have now changed the style hierarchy of ButtonBase->ToggleButton->RadioButton (the order in which styles and templates are overwritten in derived classes of WPF).

Your new hierarchy of style gives the most priority to the style of ButtonBase. So you have a new ToggleButton style that is derived from a ButtonBase and then you overwrite the RadioButton style with that. So noone can tell you for sure which properties have you overwritten and which will be the final set of properties.

To understand this change the XAML as following:

    <Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="{x:Type ToggleButton}" />
    <Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="{x:Type RadioButton}" /> 

and now see the look & feel of your RadioButton.

preview

I'm not clarifying what is exactly happened and why your ToggleButton is not affected so much. But as I said it's all about the style hierarchy which is build for a control at last just before rendering on the UI.

Upvotes: 1

Related Questions