title: WPF DataTemplateKey can not find ListBox key
body: <p>I am fairly new to WPF and am having trouble getting the DataTemplateKey to find my ListBox.</p>
  
  <pre><code>&lt;Window.Resources&gt;
  
      &lt;ControlTemplate x:Key="FocusTemplate" &gt;
          &lt;Rectangle Fill="Azure" Width="290" Height="55" /&gt;
      &lt;/ControlTemplate&gt;
      &lt;Style x:Key="FocusStyle" TargetType="{x:Type Control}"&gt;
          &lt;Setter Property="Template" Value="{StaticResource FocusTemplate}"/&gt;
      &lt;/Style&gt;
  
      &lt;Style TargetType="ListBoxItem"&gt;
          &lt;EventSetter Event="GotFocus" Handler="ListItem_GotFocus"&gt;&lt;/EventSetter&gt;
      &lt;/Style&gt;
  
      &lt;DataTemplate DataType="{x:Type TextBlock}"&gt;
      &lt;/DataTemplate&gt;
  
      &lt;DataTemplate x:Key="CustomListData" DataType="{x:Type ListBox}"&gt;
          &lt;Border BorderBrush="Black" BorderThickness="1" Margin="-2,0,0,-1"&gt;
              &lt;Grid&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="55*" /&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="Auto" /&gt;
                      &lt;ColumnDefinition Width="*" /&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;Grid.RenderTransform&gt;
                      &lt;TransformGroup&gt;
                          &lt;ScaleTransform ScaleX="1" ScaleY="1"/&gt;
                          &lt;SkewTransform AngleX="0" AngleY="0"/&gt;
                          &lt;RotateTransform Angle="0"/&gt;
                          &lt;TranslateTransform X="0" Y="0"/&gt;
                      &lt;/TransformGroup&gt;
                  &lt;/Grid.RenderTransform&gt;
                  &lt;!--&lt;ScrollViewer x:Name="PART_ContentHost" /&gt;--&gt;
                  &lt;TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent"
                                           Foreground="#FF6FB8FD"
                                           FontSize="18"
                                           FocusVisualStyle="{StaticResource FocusStyle}"
                                           Name="editingBox"
                                           TextWrapping="Wrap"
                                           Text="{Binding .}"
                                           Grid.Column="1"
                                           Grid.Row="1"
                                           MinHeight="55"
                                           Cursor="Hand"
                                           IsReadOnly="True"
                                           &gt;
                      &lt;TextBox.Background&gt;
                          &lt;LinearGradientBrush EndPoint="0,1" StartPoint="0,0"&gt;
                              &lt;LinearGradientBrush.RelativeTransform&gt;
                                  &lt;TransformGroup&gt;
                                      &lt;ScaleTransform CenterX="0.5" CenterY="0.5"/&gt;
                                      &lt;SkewTransform CenterX="0.5" CenterY="0.5"/&gt;
                                      &lt;RotateTransform Angle="0" CenterX="0.5" CenterY="0.5"/&gt;
                                      &lt;TranslateTransform/&gt;
                                  &lt;/TransformGroup&gt;
                              &lt;/LinearGradientBrush.RelativeTransform&gt;
                              &lt;GradientStop Color="#FF2D4984"/&gt;
                              &lt;GradientStop Color="#FF182D56" Offset="0.042"/&gt;
                          &lt;/LinearGradientBrush&gt;
                      &lt;/TextBox.Background&gt;
  
                  &lt;/TextBox&gt;
              &lt;/Grid&gt;
          &lt;/Border&gt;
      &lt;/DataTemplate&gt;
  
      &lt;Style TargetType="{x:Type ListBox}"&gt;
          &lt;Setter Property="ItemTemplate" Value="{StaticResource CustomListData }" /&gt;
          &lt;Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" /&gt;
      &lt;/Style&gt;
  
    &lt;/Window.Resources&gt;
  
    &lt;Window.DataContext&gt;
      &lt;ObjectDataProvider 
    ObjectType="{x:Type local:ImageLoader}" 
    MethodName="LoadImages" 
    /&gt;
    &lt;/Window.DataContext&gt;
  
      &lt;ListBox ItemsSource="{Binding}" Width="320" Background="#FF021422" BorderBrush="#FF1C4B79"&gt;
  
          &lt;ListBox.Resources&gt;
          &lt;SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"&gt;Transparent&lt;/SolidColorBrush&gt;
          &lt;/ListBox.Resources&gt;
  
      &lt;/ListBox&gt;
  </code></pre>
  
  <p>The following code will find a TextBlock</p>
  
  <pre><code>var key = new System.Windows.DataTemplateKey(typeof(TextBlock));
  var r = (DataTemplate)this.FindResource(key);
  </code></pre>
  
  <p>However, when I change the type to ListBox, the key can not be found.  What have I missed?</p>
  
  <p>Thanks
  Ryan</p>