title: Navigating up the visual tree in WPF
body: <p>I have an image being displayed in a DataTemplate. I want to change the style of the DataTemplate that's surrounding my image when the image is clicked on.</p>
  
  <p>It used to be nested directly in a stackpanel like the following so I could get the parent stackpanel easily by doing the following.</p>
  
  <pre><code>StackPanel sp = img.Parent as StackPanel;
  </code></pre>
  
  
  
  <pre><code>&lt;StackPanel Name="uxSessionImageItem" Style="{DynamicResource RotatorItemTemplateUnselectedStyle}" Loaded="uxSessionImageItem_Loaded" &gt;
       &lt;TextBlock Name="uxLabel"  Width="150" Text="{Binding SessionImageID}" Foreground="White" VerticalAlignment="Center"/&gt;
       &lt;Image MouseDown="ImagePanel_MouseDown" Name="uxImage" Style="{DynamicResource ItemTemplateImageStyle}" Source="{Binding ThumbPath}"/&gt;
  &lt;/StackPanel&gt;   
  </code></pre>
  
  <p>I had to add another stackpanel and a border in between the image and the stack panel that I need to find. What syntax do I need to find not the border that is now my parent nor the stackpanel that's the border's parent but the stackpanel above that one?</p>
  
  <pre><code>&lt;StackPanel Name="uxSessionImageItem" Style="{DynamicResource RotatorItemTemplateUnselectedStyle}" Loaded="uxSessionImageItem_Loaded" &gt;
      &lt;TextBlock Name="uxLabel"  Width="150" Text="{Binding SessionImageID}" VerticalAlignment="Center" Style="{DynamicResource ItemTemplateImageNumberStyle}"/&gt;
      &lt;StackPanel Name="uxSessionImageWrapper" Style="{DynamicResource RotatorItemImageWrapperStyle}" &gt;
          &lt;Border Name="uxImageBorder" Style="{DynamicResource ItemTemplateImageBorderStyle}"&gt;
              &lt;Image MouseDown="ImagePanel_MouseDown" Name="uxImage" Style="{DynamicResource ItemTemplateImageStyle}" Source="{Binding ThumbPath}"/&gt;
          &lt;/Border&gt;
      &lt;/StackPanel&gt;
  &lt;/StackPanel&gt;
  </code></pre>