none
TemplateBinding in Control Template Trigger? RRS feed

  • Question

  • Hi,

    today I ran into a problem when I was trying to use dependency properties of my custom control in a ControlTemplate.Trigger Setter. I can use the same properties when defining my template but when I use them in a trigger I get a exception when I try to run the application.

    Code Snippet

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:TextBoxPro}">
                        <Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                            <ScrollViewer x:Name="PART_ContentHost"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" TargetName="Bd" Value="{TemplateBinding ColorDisabledFore}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>


    The binding I marked red causes the problem. When I'm using the same property to set the default background of the border it works fine. Any ideas what I'm doing wrong?

    Phil
    Friday, April 20, 2007 10:29 AM

Answers

  • Because you're not actually in the template there, you're in its triggers, you'll need to use a full Binding I think.

     

    {Binding RelativeSource={RelativeSource Self}, Path=ColorDisabledFore}

    Friday, April 20, 2007 11:39 AM

All replies

  • Because you're not actually in the template there, you're in its triggers, you'll need to use a full Binding I think.

     

    {Binding RelativeSource={RelativeSource Self}, Path=ColorDisabledFore}

    Friday, April 20, 2007 11:39 AM
  • Thanks for pointing me in the right direction! Using RelativeSource Self will point me to the border I'm trying to change the color from. I had to change this part to get the actual object.
    Here the working binding:

    {Binding Path=ColorDisabledFore, RelativeSource={RelativeSource AncestorType={x:Type local:TextBoxPro}}}

    Thanks,
    Phil
    Friday, April 20, 2007 11:52 AM
  • This is simpler and works for me:

    {Binding RelativeSource={RelativeSource TemplatedParent}, Path=ColorDisabledFore}
    • Proposed as answer by dandax Tuesday, March 8, 2011 3:33 PM
    Saturday, August 29, 2009 3:58 PM