Monday, March 19, 2018

Preserve theme style of control while modifying the control template

Leave a Comment

I'm trying to add bind some commands to Slider.Thumb and this is how I currently do it:

<Style x:Key="BasicSliderStyle" TargetType="{x:Type customControls:ThumbDragSlider}" BasedOn="{StaticResource {x:Type Slider}}">     <Setter Property="Template">         <Setter.Value>             <ControlTemplate TargetType="{x:Type Slider}">                 <Grid>                     <Track x:Name="PART_Track">                         <Track.Thumb>                             <Thumb x:Name="Thumb">                                 <i:Interaction.Triggers>                                     <i:EventTrigger EventName="MouseEnter">                                         <command:EventToCommand Command="{Binding PositionSliderThumbMouseEnterCommand}" PassEventArgsToCommand="True"/>                                     </i:EventTrigger>                                     <i:EventTrigger EventName="DragDelta">                                         <command:EventToCommand Command="{Binding PositionThumbDragDeltaCommand}" CommandParameter="{Binding ElementName=sMovieSkipSlider}"/>                                     </i:EventTrigger>                                 </i:Interaction.Triggers>                             </Thumb>                         </Track.Thumb>                     </Track>                 </Grid>             </ControlTemplate>         </Setter.Value>     </Setter> </Style> 

The problem is that I also have a theme applied to my controls and if I use this style the style and effects from it are removed as well, is there a way to keep the current application theme style on a control while also being able to add event triggers and attach commands as show above?

P.S - Since this is a custom control that derives from Slider, this is how I set the default theme style for the slider to it

Style="{StaticResource {x:Type Slider}}" 

2 Answers

Answers 1

You can only use Triggers in the Style and not in the Template, if you want to keep the same theme for that control. That means that it is not possible in your case. What you can do is copy the entire Style of your theme and specify the trigger there.

Answers 2

You can extend your custom style. Instead of:

BasedOn="{StaticResource {x:Type Slider}}" 

do:

BasedOn="{StaticResource MyCustomSlider}" 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment