#135 – Dependency Property Value Sources: #2 – Parent Template Trigger

The second source in the list of sources for the base value of a dependency property is a parent template trigger.  A property obtains its value from a parent template trigger if the element was created as part of a template and its value is being set by a trigger defined in the template.

In the example below, the excitedLabel template has two child Label controls whose Background property changes when you hover the mouse over the parent control.  When this happens, the source of their Background property becomes the parent template trigger.

    <Window.Resources>
        <ControlTemplate x:Key="excitedLabel" TargetType="Label">
            <StackPanel Orientation="Horizontal">
                <Label Name="theYee" Content="Yee!"/>
                <ContentPresenter />
                <Label Name="theHaw" Content="Haw!"/>
            </StackPanel>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="theHaw" Property="Background" Value="HotPink"/>
                    <Setter TargetName="theYee" Property="Background" Value="HotPink"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Window.Resources>
    <StackPanel Orientation="Vertical">
        <Label Content="Run" />
        <Label Content="Skip" Template="{StaticResource excitedLabel}"/>
    </StackPanel>