Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I am creating a simple custom control extending from toggle button that allows the user to specify checked and unchecked content directly in XAML. It works well but it is based on a trigger, and I don't know how to define the trigger except in a style. If I define the style, then I lose anything set outside of the custom control.

What I would like to be able to do is just append this trigger to any existing style set elsewhere on the control.

Here's the XAML for the style/trigger.

<ToggleButton.Style>     <Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource {x:Type ToggleButton}}">         <Setter Property="Content" Value="{Binding RelativeSource={RelativeSource Self}, Path=UncheckedContent}" />         <Style.Triggers>             <Trigger Property="IsChecked" Value="True">                 <Setter Property="Content"                         Value="{Binding RelativeSource={RelativeSource Self}, Path=CheckedContent}" />             </Trigger>         </Style.Triggers>     </Style> </ToggleButton.Style> 

I tried inheriting the style via the BasedOn with a default type but it won't work if the custom control has an explicit style set by its parent. I also considered EventTriggers but I do not believe there would be an event to initialize the control.

Thanks for any help anyone can offer. :)

like image 898
Steve Cadwallader Avatar asked Sep 29 '09 22:09

Steve Cadwallader


People also ask

What is style trigger in WPF?

The WPF styling and templating model enables you to specify triggers within your Style. Essentially, triggers are objects that enable you to apply changes when certain conditions (such as when a certain property value becomes true , or when an event occurs) are satisfied.

What is trigger and how many types of triggers in WPF?

Basically, there are 3 types of triggers, they are: Property Trigger. Data Trigger. Event Trigger.

What is WPF custom control?

WPF applications allows to create custom controls which makes it very easy to create feature-rich and customizable controls. Custom controls are used when all the built-in controls provided by Microsoft are not fulfilling your criteria or you don't want to pay for third-party controls.


1 Answers

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.

like image 169
bitbonk Avatar answered Sep 21 '22 12:09

bitbonk