#140 – Dependency Property Value Sources: #6 – Template Triggers

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>
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

4 Responses to #140 – Dependency Property Value Sources: #6 – Template Triggers

  1. Pingback: Tweets that mention #140 – Dependency Property Value Sources: #6 – Template Triggers « 2,000 Things You Should Know About WPF -- Topsy.com

  2. Pingback: #133 – Where a Dependency Property Gets Its Value « 2,000 Things You Should Know About WPF

  3. Gnanasekaran says:

    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.

    • Will says:

      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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: