#136 – Dependency Property Value Sources: #3 – Parent Template

The third source in the list of sources for the base value of a dependency property is a parent template.  A property obtains its value from a parent template if the element for which the property is being set was created as part of a template and it doesn’t obtain its value through a trigger.

In the example below, the excitedLabel template has two child Label controls.  The first one has its Background property set in the template, so the source of the base value is the parent template.

    <Window.Resources>
        <ControlTemplate x:Key="excitedLabel" TargetType="Label">
            <StackPanel Orientation="Horizontal">
                <Label Name="theYee" Background="AliceBlue" Content="Yee!"/>
                <ContentPresenter />
                <Label Name="theHaw" Content="Haw!"/>
            </StackPanel>
        </ControlTemplate>
    </Window.Resources>
    <StackPanel Orientation="Vertical">
        <Label Content="Run" />
        <Label Content="Skip" Template="{StaticResource excitedLabel}"/>
    </StackPanel>

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