#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>
Pingback: Tweets that mention #140 – Dependency Property Value Sources: #6 – Template Triggers « 2,000 Things You Should Know About WPF -- Topsy.com
Pingback: #133 – Where a Dependency Property Gets Its Value « 2,000 Things You Should Know About WPF
Hi Sean, I am new to your website, i started studying WPF tips, it is very useful, i could easily understand concepts, thank you……..
I have one question regarding the Template trigger concept, i copied the above code and build project i got error message that “The member “Content” is not recognized or is not accessible.”
I mean the content property which is placed inside control template for Button element.
please assist on this.
Gnanasekaran, you probably made a mistake copying the code. If you are using Visual Studio, and created a new Window automatically (as in a new project), then you need to paste the entire code snippet INSIDE the grid. Visual Studio is telling you that the Grid does not have a Content property.