Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use custom properties in a ControlTemplate trigger

Tags:

wpf

I have a class derived from slider which uses a custom control template and has a few added dependency properties. I would like to fire triggers within the template based on the new properties.

For example, I have a new dependency property called ThumbIsVisible which when set to false I want just the thumb portion of my slider to be hiddin. My control template looks like:

<Slider.Template>
<ControlTemplate TargetType="{x:Type Slider}">
...
<Track.Thumb>
<Thumb x:Name="m_Thumb" Style="{StaticResource SliderThumbStyle}" />
...

I would like to add in a trigger that looks like:

<ControlTempate.Trigger>
<Trigger Property="ThumbIsVisible" Value="False">
<Setter TargetName="m_Thumb" Property="Visibility" Value="Collapsed" />

Right off the bat I can see this won't work as I have the control tempate's target type set to Slider. However, if I change that to say:

<ControlTemplate TargetType="{x:Type local:myCustomSlider}">

then I run into problems with the template type differing from the controls. The only way around this is to create the xaml using the local:myCustomSlider as the type instead of Slider. However, doing this causes lots of problems with VisualStudio's designer and code behind.

Does anyone know if there is a standard way to get around all of this? As a workaround I tried adding to the template's triggers via code-behind but have not been able to get that to work.

like image 436
BruceLH Avatar asked Feb 28 '12 16:02

BruceLH


1 Answers

It looks like there is an even simpler way to solve this problem.

<ControlTemplate.Triggers>   
  <Trigger Property=local:CustomSlider.ThumbIsVisible" Value="False">     
    <Setter TargetName="m_Thumb" Property="Visibility" Value="Hidden" />
  </Trigger> 
</ControlTemplate.Triggers> 

where local is the namespace of the CustomSlider class.

like image 80
BruceLH Avatar answered Jan 02 '23 06:01

BruceLH