#140 – Dependency Property Value Sources: #6 – Template Triggers
November 29, 2010 4 Comments
The sixth source in the list of sources for the base value of a dependency property is a template trigger. A property obtains its value from a template trigger when the element that the property belongs to has a template containing a trigger that changes the property.
In the example below, we apply a template to the main button, replacing it with two labels and a new button. The trigger changes the Foreground property of the original button, so the property source for that property becomes template trigger.
The original button is replaced by the controls in the template, but its Foreground property is preserved by using the TemplateBinding markup extension.
<Button Name="btnWithTemplate" Content="Recreate Me"> <Button.Template> <ControlTemplate TargetType="{x:Type Button}"> <StackPanel Orientation="Horizontal"> <Label Content="**" Foreground="{TemplateBinding Foreground}"/> <Button Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}"/> <Label Content="**" Foreground="{TemplateBinding Foreground}"/> </StackPanel> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Foreground" Value="Green"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Button.Template> </Button>