title: How to find a resource in a UserControl from a DataTemplateSelector class in WPF?
body: <p>I'm creating my own UserControl and I have two different DataTemplates under the <strong>UserControl.Resources</strong> section in my XAML. I want to choose between these two datatemplates depending on the value of a property on objects displayed in a listview. I do this by creating a custom <strong>DataTemplateSelector</strong> class and overriding the <strong>SelectTemplate</strong> method which is supposed to return the DataTemplate I wish to use. However, I have no idea how to "find" my datatemplates that are located in the UserControls resource section, all the examples I've seen only fetches datatemplates from <strong>Window.Resources</strong>. In this example they fetch the current <strong>MainWindow</strong> and then use <strong>FindResource</strong> to find the <strong>DataTemplate</strong>, how do I fetch my <strong>UserControl</strong> in a similar manner?:</p>
  
  <pre><code>
  public override DataTemplate 
              SelectTemplate(object item, DependencyObject container)
          {
              if (item != null && item is AuctionItem)
              {
                  AuctionItem auctionItem = item as AuctionItem;
                  Window window = Application.Current.MainWindow;
  
                  switch (auctionItem.SpecialFeatures)
                  {
                      case SpecialFeatures.None:
                          return 
                              window.FindResource("AuctionItem_None") 
                              as DataTemplate;
                      case SpecialFeatures.Color:
                          return 
                              window.FindResource("AuctionItem_Color") 
                              as DataTemplate;
                  }
              }
  
              return null;
          }
  </code></pre>
  
  <p>The example above is from here: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplateselector.aspx" rel="nofollow noreferrer">ItemsControl.ItemTemplateSelector Property </a></p>