title: How do I access to a control element that is defined in a style
body: <p>I've a CustomControl which use a ScrollViewer. The following code is an example which shows my use case in an easy way:</p>
  
  <pre><code>&lt;Style TargetType="controls:MyCustomControl"&gt;
    &lt;Setter Property="Template"&gt;
      &lt;Setter.Value&gt;
        &lt;ControlTemplate TargetType="controls:MyCustomControl"&gt;
          &lt;Grid x:Name="LayoutRoot" Background="White"&gt;
            &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition /&gt;
              &lt;RowDefinition Height="20"/&gt;
              &lt;RowDefinition Height="20"/&gt;
            &lt;/Grid.RowDefinitions&gt;
            &lt;ScrollViewer x:Name="scrollViewer" Grid.Row="0"
                          Height="300" Width="400"
                          VerticalScrollBarVisibility="Auto" 
                          HorizontalScrollBarVisibility="Auto"
                          Style="{StaticResource ScrollViewerStyle}"&gt;
              &lt;Canvas x:Name="outer" Width="400" Height="400"
                      Background="Red" Margin="50"&gt;
                &lt;Canvas x:Name="inner" Background="Green" 
                        Width="400" Height="400"&gt;
                  &lt;Canvas.RenderTransform&gt;
                    &lt;ScaleTransform ScaleX="{Binding Value, ElementName=slider}"
                                    ScaleY="{Binding Value, ElementName=slider}"/&gt;
                  &lt;/Canvas.RenderTransform&gt;
                &lt;/Canvas&gt;
              &lt;/Canvas&gt;
            &lt;/ScrollViewer&gt;
          &lt;Slider x:Name="slider" Minimum="0.1" Maximum ="10"
                  Value="1" Grid.Row="2"/&gt;
        &lt;/Grid&gt;
      &lt;/ControlTemplate&gt;
    &lt;/Setter.Value&gt;
  &lt;/Setter&gt;
  </code></pre>
  
  <p></p>
  
  <p>I left out the nonrelevant CodeBehind belonging to Slider.ValueChanged.</p>
  
  <p>In a further question I ask <a href="https://stackoverflow.com/questions/7417844/how-do-i-stick-controls-inside-of-scrollviewer">How do I stick Controls inside of ScrollViewer</a>. In this answer I got the idea to template the ScrollViewer. I added some elements to the ScrollViewer as shown in the answer. For example I've a new Canvas inside of my ScrollViewer:</p>
  
  <p><code>&lt;Canvas x:Name="canvas"/&gt;</code></p>
  
  <p>I need access to this Canvas at runtime, so in WPF I created:</p>
  
  <p><code>public class MyScrollViewer : ScrollViewer</code></p>
  
  <p>I override <code>OnApplyTemplate</code> and get access to it via <code>GetTemplateChild</code>. So all done.</p>
  
  <p>In SL this way is not possible since ScrollViewer is sealed.</p>
  
  <p>Now I am looking for an other way to get access. I hoped VisualTreeHelper would help me, but I do not get the Canvas. Neither in CodeBehind of my custom control nor in the codebehind of my MainPage (where I use my control).</p>
  
  <pre><code>private void GetCanvas(DependencyObject dependencyObject)
    {
      int count = VisualTreeHelper.GetChildrenCount(dependencyObject);
      for (int i = 0; i &lt; count; i++)
      {
        DependencyObject child = VisualTreeHelper.GetChild(dependencyObject);
        Canvas canvas = child as Canvas;
        if (canvas == null)
        {
          GetCanvas(child);
        }
        else
        {
          // Code here if found
        }
      }
    }
  </code></pre>
  
  <p>Then a colleague remember me, that there exists the directive x:FieldModifier. I never used it before, just heard about it. I give it a try.</p>
  
  <pre><code>x:FieldModifier="public"
  </code></pre>
  
  <p>But no idea if it really give me the possibility to access from codebehind, even less how.</p>
  
  <p>So, is there any way how I could access at my Canvas at runtime in Silverlight? Maybe there is maybe another solution in WPF itself, which also works on SL?</p>