How to add a trigger to a WPF custom control without overriding the existing style? How to add a trigger to a WPF custom control without overriding the existing style? wpf wpf

How to add a trigger to a WPF custom control without overriding the existing style?


Just to clear things up on the terminology here: A user control is a control that derives from the UserControl class. If I understood you right you derived from ToggleButton to add the UncheckedContent and CheckedContent properties. In that case you have created a custom control. It's always easier to follow if we agree on common terminology :)

As far as I know you can not do such a generic style inheritance in XAML. You always have to specify explicitly what style a another style is based upon. Your style can either be based on the default style for ToggleButton or on a specific other style. If you can't build a style inheritance chain that respects that, this approach won't work.

But since you have a custom control, couldn't you write a default style for it that is based on the default toggle button style like this?

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

Then whenever you apply an explicit style to a toggle button you would specify that it is based on the default toggle button style.

Also you could write a (default) control template for your new toggle button in Themes\Generic.xaml that contains the above triggers. In blend you can get a copy of the default template for toggle button ("Edit Template"->"Edit a Copy") so you can make sure that your toggle button looks exactly like the normal toggle button. Then incorporate the triggers above into that template.

BTW: you do not have to create a new control just to add new properties. You can add new properties to an existing control using attached properties. They can be used from XAML just like normal properties.