Search code examples
wpfwpf-style

Deriving style from base style fails WPF


I just realized that deriving from default style fails in my WPF app and I have no idea, why. Actually it works, but only due to "Hot reload". So I have:

<Style TargetType="ComboBox" x:Key="TestStyle" BasedOn="{StaticResource {x:Type ComboBox}}"/>

<Style TargetType="ComboBox">
    <Setter Property="Width" Value="100"/>
</Style>

in resource dictionary, and:

<ComboBox Style="{StaticResource TestStyle}">
    <ComboBoxItem>test</ComboBoxItem>
    <ComboBoxItem>I want to cry with blood</ComboBoxItem>
</ComboBox>

in my control. When I start app I see following:

incorrect view

And when I remove BasedOn="{StaticResource {x:Type ComboBox}}" and add it again, I have correct view:

enter image description here



What can be a reason of such behavior? Seems like WPF bug, but I don't think this is possible

Solution

  • To find correct style you need to define it before "TestStyle"

    <Style TargetType="ComboBox">
        <Setter Property="Width" Value="100"/>
    </Style>
    
    <Style TargetType="ComboBox" x:Key="TestStyle" BasedOn="{StaticResource {x:Type ComboBox}}"/>