title: Why won't WPF choose the right DataTemplate in my case?
body: <p>I have 4 classes in my ViewModel that implements the IPage interface (an interface I created). In the ViewModel of my app I've a CurrentPage Property of IPage type.</p>
  
  <p>In the View layer of my app, I've a ContentControl which Content is bound to the CurrentPage property.</p>
  
  <p>So, when I'm using the following code WPF won't find the right DataTemplate</p>
  
  <pre><code>&lt;UserControl ...&gt;
      &lt;ContentControl Content="{Binding CurrentPage, Mode=OneWay}"
                      VerticalAlignment="Stretch" HorizontalAlignment="Stretch"&gt;
          &lt;ContentControl.Resources&gt;
              &lt;DataTemplate DataType="{x:Type operation:OperationListPageVM}"&gt;
                  &lt;pages1:OperationListPageV/&gt;
              &lt;/DataTemplate&gt;
              &lt;DataTemplate DataType="{x:Type operation:OperationDetailPageVM}"&gt;
                  &lt;pages1:OperationDetailPageV/&gt;
              &lt;/DataTemplate&gt;
              &lt;DataTemplate DataType="{x:Type part:PartListPageVM}"&gt;
                  &lt;pages1:PartListPageV/&gt;
              &lt;/DataTemplate&gt;
              &lt;DataTemplate DataType="{x:Type part:PartDetailPageVM}"&gt;
                  &lt;pages1:PartDetailPageV/&gt;
              &lt;/DataTemplate&gt;
              &lt;DataTemplate DataType="{x:Type pages:SettingsPageVM}"&gt;
                  &lt;pages1:SettingsPageV/&gt;
              &lt;/DataTemplate&gt;
          &lt;/ContentControl.Resources&gt;
      &lt;/ContentControl&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>But if I only put one DataTemplate as ContentControl.ContentTemplate then it'll apply it correctly</p>
  
  <pre><code>&lt;UserControl ...&gt;
      &lt;ContentControl Content="{Binding CurrentPage, Mode=OneWay}"
                      VerticalAlignment="Stretch" HorizontalAlignment="Stretch"&gt;
          &lt;ContentControl.ContentTemplate&gt;
              &lt;DataTemplate DataType="{x:Type operation:OperationListPageVM}"&gt;
                  &lt;pages1:OperationListPageV/&gt;
              &lt;/DataTemplate&gt;
          &lt;/ContentControl.ContentTemplate&gt;
      &lt;/ContentControl&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>If I then put those DataTemplates back as resources, give them a key and create a DataTemplateSelector and add </p>
  
  <pre><code>ContentTemplateSelector="{StaticResource PageTemplateSelector}"
  </code></pre>
  
  <p>To the ControlTemplate with the DataTemplateSelector as this:</p>
  
  <pre><code>using System.Windows;
  using System.Windows.Controls;
  using MaintenanceModuleV3.ViewModel;
  using MaintenanceModuleV3.ViewModel.Pages;
  using MaintenanceModuleV3.ViewModel.Pages.Operation;
  using MaintenanceModuleV3.ViewModel.Pages.Part;
  
  namespace MaintenanceModuleV3.View.TemplateSelector {
      public class PageTemplateSelector : DataTemplateSelector {
          public override DataTemplate SelectTemplate(object item, DependencyObject container) {
              if (AppVM.Instance.CurrentPage != null) {
                  if (AppVM.Instance.CurrentPage is OperationListPageVM) {
                      return (container as FrameworkElement).FindResource("OperationListPageTemplate") as DataTemplate;
                  } else if (AppVM.Instance.CurrentPage is OperationDetailPageVM) {
                      return (container as FrameworkElement).FindResource("OperationDetailPageTemplate") as DataTemplate;
                  } else if (AppVM.Instance.CurrentPage is PartListPageVM) {
                      return (container as FrameworkElement).FindResource("PartListPageTemplate") as DataTemplate;
                  } else if (AppVM.Instance.CurrentPage is PartDetailPageVM) {
                      return (container as FrameworkElement).FindResource("PartDetailPageTemplate") as DataTemplate;
                  } else if (AppVM.Instance.CurrentPage is SettingsPageVM) {
                      return (container as FrameworkElement).FindResource("SettingsPageTemplate") as DataTemplate;
                  }
              }
              return null;
          }
      }
  }
  </code></pre>
  
  <p>Then the right DataTemplate is choosen.</p>
  
  <p>Why won't WPF choose the right DataTemplate if I put DataTemplates in the element resources in this case?</p>
  
  <p>Thank you</p>
  
  <p>NB: Using Framework 3.5</p>
  
  <p>EDIT: I noticed a second problem: the DataContext inside the DataTemplates are null. Why is that? Why won't they take CurrentPage as DataContext? This is the case when I directly set the ContentTemplate too. WPF seems not to like bindings to interface typed properties.</p>
  
  <p>EDIT 2: If I set the DataContext of the OperationListPageV explicitly in its constructor and use TemplateSelector everything works, but why do I have to use those?</p>
  
  <p>EDIT 3: The full ViewModel code, if it helps:</p>
  
  <p><strong>The app ViewModel</strong></p>
  
  <pre><code>public class AppVM : INotifyPropertyChanged {
  
      #region constructor
      private AppVM() {
          AppM.LanguageChanged += (sender, args) =&gt; {
              onPropertyChanged("CurrentLanguage");
          };
  
          AppM.NbDaysAvgChanged += (sender, args) =&gt; {
              onPropertyChanged("NbDaysAvg");
          };
  
          AppM.NbDaysMenuNotificationChanged += (sender, args) =&gt; {
              onPropertyChanged("NbDaysMenuNotification");
          };
      }
      #endregion
  
      #region business
  
      #endregion
  
      #region events
      public event PropertyChangedEventHandler PropertyChanged;
  
      protected virtual void onPropertyChanged(string propertyName) {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
      }
      #endregion
  
      #region properties
  
      private static AppVM _instance;
      public static AppVM Instance {
          get { return _instance ?? (_instance = new AppVM()); }
      }
  
  
      #region notifypropertychanged
      private OperationListPageVM _currentPage = OperationListPageVM.Instance;
      public OperationListPageVM CurrentPage {
          get { return _currentPage; }
          set {
              _currentPage = value;
              onPropertyChanged("CurrentPage");
          }
      }
  
      public Language CurrentLanguage {
          get { return AppM.CurrentLanguage; }
      }
  
      public int NbDaysMenuNotification {
          get { return AppM.NbDaysMenuNotification; }
      }
  
      public int NbDaysAvg {
          get { return AppM.NbDaysAvg; }
      }
  
      public Dictionary&lt;String, String&gt; AvailableTranslations {
          get { return AppM.AvailableTranslations; }
      }
      #endregion
      #region commands
      #endregion
      #endregion
  }
  </code></pre>
  
  <p><strong>The page I was to display ViewModel</strong></p>
  
  <pre><code>public sealed class OperationListPageVM : INotifyPropertyChanged, IPage {
  
      #region constructor
      private OperationListPageVM() {
          //fill the available operations with data stored in the App Model coming from the DataBase
          foreach (MaintenanceOperation operation in AppM.Operations) {
              AvailableOperations.Add(new MaintenanceOperationVM(operation)); 
          }
          foreach (MaintenanceOperationVM operation in AvailableOperations) {
              DisplayedOperations.Add(operation);
          }
      }
      #endregion
  
      #region business
  
      #endregion
  
      #region events
      public event PropertyChangedEventHandler PropertyChanged;
  
      private void onPropertyChanged(string propertyName) {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
      }
      #endregion
  
      #region properties
  
      private static OperationListPageVM _instance;
  
      public static OperationListPageVM Instance {
          get { return _instance ?? (_instance = new OperationListPageVM()); }
      }
  
      #region notifypropertychanged
      private ObservableCollection&lt;MaintenanceOperationVM&gt; _displayedOperations = new ObservableCollection&lt;MaintenanceOperationVM&gt;();
      public ObservableCollection&lt;MaintenanceOperationVM&gt; DisplayedOperations {
          get { return _displayedOperations; }
          set {
              _displayedOperations = value;
              onPropertyChanged("DisplayedOperations");
          }
      }
  
      private readonly List&lt;MaintenanceOperationVM&gt; _availableOperations = new List&lt;MaintenanceOperationVM&gt;();
  
      internal List&lt;MaintenanceOperationVM&gt; AvailableOperations {
          get { return _availableOperations; }
      }
  
      #endregion
  
      #region commands
  
      #endregion
  
      #endregion
  }
  </code></pre>
  
  <p><strong>The View layer of the page I was to display (OperationListPageV)</strong></p>
  
  <pre><code>&lt;UserControl x:Class="MaintenanceModuleV3.View.Pages.OperationListPageV"
              ...&gt;
      &lt;touch:ScrollFriction2&gt;
          &lt;ListBox ItemsSource="{Binding DisplayedOperations}"&gt;
                  &lt;ListBox.ItemContainerStyle&gt;
                      &lt;Style TargetType="{x:Type ListBoxItem}"&gt;
                          &lt;Setter Property="HorizontalContentAlignment" Value="Stretch"/&gt;
                      &lt;/Style&gt;
                  &lt;/ListBox.ItemContainerStyle&gt;
              &lt;ListBox.ItemTemplate&gt;
                  &lt;DataTemplate DataType="{x:Type viewModel:MaintenanceOperationVM}"&gt;
                      &lt;view:MaintenanceOperationV/&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListBox.ItemTemplate&gt;
          &lt;/ListBox&gt;
      &lt;/touch:ScrollFriction2&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>If you need more code just ask for it. Thank you!</p>