Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

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 question from:https://stackoverflow.com/questions/66048560/deriving-style-from-base-style-fails-wpf

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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}}"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...