title: Control Template Binding Error
body: <p>Below is part of a style for a TabItem, for which I am getting binding errors similar to the one shown below.</p>
  
  <p>How can I clean the code up?</p>
  
  <h1>Error</h1>
  
  <pre><code>System.Windows.Data Error: 40 : BindingExpression path error: 'HoverForeground' property not found on 'object' ''TabItem' (Name='')'. BindingExpression:Path=HoverForeground; DataItem='TabItem' (Name=''); target element is 'GlyphButton' (Name='HideButton'); target property is 'Foreground' (type 'Brush')
  </code></pre>
  
  <h1>TabItem style</h1>
  
  <pre><code>&lt;Style x:Key="OrangeTabItemStyle" TargetType="{x:Type TabItem}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type TabItem}"&gt;
                  &lt;Border AllowDrop="true" ToolTip="{Binding DisplayName}"&gt;
                      &lt;Border Name="Border" Background="Transparent" BorderBrush="Transparent" BorderThickness="1,1,1,0" CornerRadius="2,2,0,0"&gt;
                          &lt;DockPanel x:Name="TitlePanel" TextElement.Foreground="{StaticResource FileTabTextBrush}"&gt;
                 ******             &lt;ctrl:GlyphButton x:Name="HideButton" 
                          DockPanel.Dock="Right" 
                          GlyphForeground="Transparent" 
                          HoverBackground="{StaticResource ToolWindowButtonHoverActiveBackgroundBrush}" 
                          HoverBorderBrush="{StaticResource ToolWindowButtonHoverActiveBorderBrush}" 
                *****          HoverForeground="{StaticResource ToolWindowButtonHoverActiveGlyphForegroundBrush}" 
                          PressedBackground="{StaticResource ToolWindowButtonPressedBackgroundBrush}" 
                          PressedBorderBrush="{StaticResource ToolWindowButtonPressedBorderBrush}" 
                          PressedForeground="{StaticResource ToolWindowButtonPressedActiveGlyphForegroundBrush}"
                          HoverBorderThickness="1" PressedBorderThickness="1" Margin="3,2,3,4" 
                          Command="{Binding CloseCommand}"
                          CommandParameter="{Binding}"
                          ToolTip="Close"&gt;
                                  &lt;Path x:Name="CloseButtonStroke" Width="10" Height="8" Stretch="Uniform" 
                                        Data="F1 M 0,0 L 2,0 5,3 8,0 10,0 6,4 10,8 8,8 5,5 2,8 0,8 4,4 0,0 Z" 
                              Fill="{Binding Path=(TextElement.Foreground), RelativeSource={RelativeSource Self}}" /&gt;
                              &lt;/ctrl:GlyphButton&gt;
  
                              &lt;ContentPresenter x:Name="Content" HorizontalAlignment="Stretch" Margin="4,2,4,4" VerticalAlignment="Stretch" 
                                                RecognizesAccessKey="true" ContentSource="Header" /&gt;
                          &lt;/DockPanel&gt;
                      &lt;/Border&gt;
                      &lt;Border.InputBindings&gt;
                          &lt;KeyBinding Command="{Binding CloseCommand}" Key="F4" Modifiers="Control" /&gt;
                      &lt;/Border.InputBindings&gt;
                  &lt;/Border&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsMouseOver" Value="true"&gt;
                          &lt;Setter TargetName="Border" Value="{StaticResource FileTabIsMouseOverBorderBackgroundBrush}" Property="Background" /&gt;
                          &lt;Setter TargetName="Border" Value="{StaticResource FileTabIsMouseOverBorderBrush}" Property="BorderBrush" /&gt;
                          &lt;Setter TargetName="TitlePanel" Value="{StaticResource FileTabIsMouseOverTextForegroundBrush}" Property="TextElement.Foreground" /&gt;
                          &lt;Setter TargetName="HideButton" Value="{StaticResource FileTabIsMouseOverGlyphForegroundBrush}" Property="GlyphForeground" /&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsSelected" Value="True"&gt;
                          &lt;Setter Property="Panel.ZIndex" Value="1" /&gt;
                          &lt;Setter TargetName="Border" Value="{StaticResource FileTabIsSelectedBackgroundBrush}" Property="Background" /&gt;
                          &lt;Setter TargetName="Border" Value="{StaticResource FileTabIsSelectedBackgroundBrush}" Property="BorderBrush" /&gt;
                          &lt;Setter TargetName="Border" Property="BorderThickness" Value="0" /&gt;
                          &lt;Setter TargetName="Border" Property="Padding" Value="0,1,0,0" /&gt;
                          &lt;Setter TargetName="HideButton" Property="Margin" Value="3" /&gt;
                          &lt;Setter TargetName="TitlePanel" Value="{StaticResource FileTabIsSelectedTextForegroundBrush}" Property="TextElement.Foreground" /&gt;
                          &lt;Setter TargetName="HideButton" Value="{StaticResource FileTabIsSelectedGlyphForegroundBrush}" Property="GlyphForeground" /&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <h1>GlyphButton control</h1>
  
  <pre><code>public class GlyphButton : Button
  {
  
      public static readonly DependencyProperty HoverForegroundProperty = DependencyProperty
          .Register("HoverForeground", typeof(Brush), typeof(GlyphButton));
  
      public Brush HoverForeground
      {
          get { return (Brush) GetValue(HoverForegroundProperty); }
          set { SetValue(HoverForegroundProperty, value); } 
      }
  
      // other properties similar to this omitted....
  }
  </code></pre>
  
  <h1>EDIT - GlyphButton Style</h1>
  
  <pre><code>&lt;Style TargetType="{x:Type ctrl:GlyphButton}"&gt;
      &lt;Setter Property="Width" Value="{Binding Path=ActualHeight, RelativeSource={RelativeSource Self}}" /&gt;
      &lt;Setter Property="Foreground" Value="{Binding Path=GlyphForeground, RelativeSource={RelativeSource Self}}" /&gt;
      &lt;Setter Property="Background" Value="Transparent" /&gt;
      &lt;Setter Property="BorderBrush" Value="Transparent" /&gt;
      &lt;Setter Property="BorderThickness" Value="1" /&gt;
      &lt;Setter Property="Focusable" Value="false" /&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type ctrl:GlyphButton}"&gt;
                  &lt;Border 
                      Name="Border" Background="{TemplateBinding Background}" 
                      BorderBrush="{TemplateBinding BorderBrush}" 
                      BorderThickness="{TemplateBinding BorderThickness}"&gt;
                      &lt;ContentPresenter /&gt;
                  &lt;/Border&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsMouseOver" Value="true"&gt;
                          &lt;Setter TargetName="Border" Value="{Binding HoverBackground , RelativeSource={RelativeSource TemplatedParent}}" 
                                  Property="Background" /&gt;
                          &lt;Setter TargetName="Border" Value="{Binding HoverBorderBrush , RelativeSource={RelativeSource TemplatedParent}}" 
                                  Property="BorderBrush" /&gt;
                          &lt;Setter TargetName="Border" Value="{Binding HoverBorderThickness , RelativeSource={RelativeSource TemplatedParent}}" 
                                  Property="BorderThickness" /&gt;
                          &lt;Setter Value="{Binding HoverForeground , RelativeSource={RelativeSource TemplatedParent}}" 
                                  Property="Foreground" /&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsPressed" Value="True"&gt;
                          &lt;Setter TargetName="Border" Value="{Binding PressedBackground , RelativeSource={RelativeSource TemplatedParent}}" 
                                  Property="Background" /&gt;
                          &lt;Setter TargetName="Border" Value="{Binding PressedBorderBrush , RelativeSource={RelativeSource TemplatedParent}}" 
                                  Property="BorderBrush" /&gt;
                          &lt;Setter TargetName="Border" Value="{Binding PressedBorderThickness , RelativeSource={RelativeSource TemplatedParent}}" 
                                  Property="BorderThickness" /&gt;
                          &lt;Setter Value="{Binding PressedForeground , RelativeSource={RelativeSource TemplatedParent}}" 
                                  Property="Foreground" /&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>