title: Altering a ControlTemplate from code behind
body: <p><strong>Application:</strong></p>
  
  <p>Hello, I am dynamically adding custom controls to a WPF application. The control is a custom slider. I have created a ControlTemplate in the XAML file which I want to use as the template for these dynamically created controls.  I am currently applying the template by using:</p>
  
  <pre><code>newControl.Template = (ControlTemplate)parent.Resources["nameOfTheControlTemplate"];
  </code></pre>
  
  <p>This currently works OK (i.e. compiles, runs, and applys the template).</p>
  
  <p>The template looks like this: ( Sorry for wall of text )</p>
  
  <pre><code>&lt;ControlTemplate x:Key="errorRangeSliderRight" TargetType="{x:Type Slider}"&gt;
  &lt;Border SnapsToDevicePixels="true" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"&gt;
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}"/&gt;
              &lt;RowDefinition Height="Auto"/&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Track x:Name="PART_Track" Grid.Row="1"&gt;
              &lt;Track.Thumb&gt;
                  &lt;Thumb x:Name="Thumb" HorizontalContentAlignment="Right" Width="7"&gt;
                      &lt;Thumb.Template&gt;
                          &lt;ControlTemplate TargetType="Thumb"&gt;
                              &lt;Path x:Name="nameOfPath" Stroke="Black" StrokeThickness="0" Fill="Red"&gt;
                                  &lt;Path.Data&gt;
                                      &lt;GeometryGroup FillRule="NonZero"&gt;
                                          &lt;PathGeometry&gt;
                                              &lt;PathGeometry.Figures&gt;
                                                  &lt;PathFigure IsClosed="True" StartPoint="7,150"&gt;
                                                      &lt;PathFigure.Segments&gt;
                                                          &lt;PathSegmentCollection&gt;
                                                              &lt;LineSegment Point="5,150" /&gt;
                                                              &lt;LineSegment Point="5,0" /&gt;
                                                              &lt;LineSegment Point="7,0" /&gt;
                                                          &lt;/PathSegmentCollection&gt;
                                                      &lt;/PathFigure.Segments&gt;
                                                  &lt;/PathFigure&gt;
                                                  &lt;PathFigure IsClosed="True" StartPoint="0,75"&gt;
                                                      &lt;PathFigure.Segments&gt;
                                                          &lt;PathSegmentCollection&gt;
                                                              &lt;LineSegment Point="7,70" /&gt;
                                                              &lt;LineSegment Point="7,80" /&gt;
                                                              &lt;LineSegment Point="0,75" /&gt;
                                                          &lt;/PathSegmentCollection&gt;
                                                      &lt;/PathFigure.Segments&gt;
                                                  &lt;/PathFigure&gt;
                                              &lt;/PathGeometry.Figures&gt;
                                          &lt;/PathGeometry&gt;
                                      &lt;/GeometryGroup&gt;
                                  &lt;/Path.Data&gt;
                              &lt;/Path&gt;
                          &lt;/ControlTemplate&gt;
                      &lt;/Thumb.Template&gt;
                  &lt;/Thumb&gt;
              &lt;/Track.Thumb&gt;
          &lt;/Track&gt;
      &lt;/Grid&gt;
  &lt;/Border&gt;
  &lt;/ControlTemplate&gt;
  </code></pre>
  
  <p><strong>Reasoning:</strong></p>
  
  <p>The reason why I chose to define the control template in the XAML rather than dynamically create a template using millions of lines of code using FrameworkElementFactory's is because it is easier, cleaner, and easier to maintain/read.</p>
  
  <p><strong>What I want:</strong></p>
  
  <p>I would like to make slight alterations to this control template ( only the Fill color of a Path object ) within the ControlTemplate. If it is possible I would like to get a reference to the ControlTemplate object.</p>
  
  <p><strong>What I have tried:</strong></p>
  
  <p>I have tried calling a FindName("nameOfPath") on the Template, it returns a null object.</p>
  
  <pre><code>Object o = newControl.Template.FindName("nameOfPath",newControl);
  </code></pre>
  
  <p>I have tried creating the ContentTemlpate using lots of FrameworkElementFactory instances and building the ControlTemplate that way, this was unsuccessful ( the ControlTemplate object is quite complex and has many child elements).</p>