I want to apply the following style to all controls that derive from ButtonBase
<Style
TargetType="{x:Type ButtonBase}">
<Setter
Property="Cursor"
Value="Hand" />
</Style>
But it only works for a given class, not for its descendants. How to achieve what I intend to?
This doesn’t work because when an element doesn’t have a style explicitly assigned, WPF finds its style by calling
FindResource, using the element’s type as the key. The fact that you’ve created a style whose key isButtonBasedoesn’t matter: WPF finds the style with the key ofButtonorToggleButtonand uses it.An inheritance-based lookup method would look up the style using the element’s type, and then use the base type if no style for the element’s type was found (and keep on going until it either found a style or hit
FrameworkElement). The problem is that only works if no match is found – i.e. if there’s no default style forButton, which of course there is.There are two things you can do. One is to do what Jens suggests, using the style’s
BasedOnproperty to implement your own hierarchy of styles. That’s kind of annoying, though, because you have to define a style for every single type; if you don’t, the default WPF style for that type will be used.Another way is to use a
StyleSelectorthat implements this lookup behavior. Like this:You can create an instance of this, give it a set of styles, and then attach it to any
ItemsControl: