If I have a subclass of ContextMenu
(for example) called MyContextMenu
and I define a style in my MergedDictionaries
with TargetType=ContextMenu
, why does this style not apply to instances of MyContextMenu
?
It does work for all of the instances of the base class, but not any of the subclasses.
I can create additional styles and use BasedOn
, but that isn't clean. It seems like it should work without that.
I have made sure that my subclasses call the base constructor, and have verified DefaultStyleKey
to be the type of my base class.
According to this article:
Note, that the TargetType must match exactly for a typed style to be applied. For example, if you specify the Style’s Key, then it’s ok for the target element to be a subclass of the TargetType. But a typed style typically gets applied to elements which type matches exactly! This is done to prevent surprises. For example, you might have created a Style for all ToggleButtons in your application and you don’t want this style to be applied to any CheckBoxes (which derives from the ToggleButton).
The same can be read in the official documentation (halfway down)