title: WPF: When changing a ContentControl's Content, the new content object is NOT firing the loaded event
body: <p><strong>Problem:</strong></p>
  
  <p>I am unable to hook into the point in time after I change a ContentControl's content, the DataTemplate from a DataTemplateSelector has been applied, and all visual layout has been completed, i.e. the new content is Loaded.</p>
  
  <p><strong>Setup:</strong></p>
  
  <p>A custom control called <em>'KeyboardHost'</em> which extends a <em>ContentControl</em>.</p>
  
  <p><em>ContentControl.Content</em> is bound (using a multi-binding) to 2 notification properties, via a value converter which combines the 2 bound properties into an object of type 'KeyboardCriteria'.</p>
  
  <p><em>'KeyboardCriteria'</em> is a public class, but I have also tried making this a FrameworkElement, Control and UserControl so that I could try to hook into the Initialized, Loaded etc events.</p>
  
  <p><em>ContentControl.ContentTemplateSelector</em> is a custom selector class (below) which returns a 
  DataTemplate based on the ContentControl.Content (the 'KeyboardCriteria').</p>
  
  <p><em>The ContentControl.ContentTemplateSelector's DataTemplates are properties on the selector and are initialised and assigned in the resources section of my MainView.</em></p>
  
  <p><strong>Attempts:</strong></p>
  
  <p>I have attached/overridden the following ContentControl events:</p>
  
  <pre><code>Initialized
  
  Loaded
  
  OnContentChanged
  
  OnContentTemplateChanged
  </code></pre>
  
  <p>I have attached/override the following 'KeyboardCriteria' (defined as a FrameworkElement) events:</p>
  
  <pre><code>Initialized
  
  Loaded
  
  OnApplyTemplate
  
  OnTemplateChanged
  
  TemplateDP callback
  </code></pre>
  
  <p><strong>Observations:</strong></p>
  
  <p>On startup:</p>
  
  <pre><code>KeyboardHost: OnTemplateChanged
  
  KeyboardHost: ContentChanged
  
  KeyboardCriteria: Initialized
  
  KeyboardCriteria: Loaded
  </code></pre>
  
  <p>When changing one of the bound criteria properties (thus creating a new KeyboardCriteria object):</p>
  
  <pre><code>KeyboardHost: ContentChanged
  
  KeyboardCriteria: Initialized
  </code></pre>
  
  <p><strong><em>N.B. The lack of a Loaded event on the ContentControl.Content object (the 'KeyboardCriteria').</em></strong></p>
  
  <p><strong>Next steps:</strong></p>
  
  <p>I think I will scrap the idea of using a DataTemplateSelector entirely and build the selection logic into my ContentControl, as this is already a CustomControl. I am hoping by manually creating the Content (and populating it) I can avoid the use of the DataTemplates that I currently use in the selection logic, as I suspect this is part of the problem.</p>
  
  <p>...</p>
  
  <p><strong><em>CODE SAMPLES:</em></strong></p>
  
  <p><strong>MainViewModel:</strong></p>
  
  <p>Exposes 'Keyboard' property, which initially has a non-null value.</p>
  
  <p><strong>MainView:</strong></p>
  
  <pre><code>&lt;controls:KeyboardHost Grid.Row="0" 
                         ContentTemplateSelector="{StaticResource KeyboardDataTemplateSelector}"&gt;
      &lt;ContentControl.Content&gt;
          &lt;MultiBinding Converter="{StaticResource KeyboardCriteriaValueConverter}" Mode="OneWay"&gt;
              &lt;Binding Source="{x:Static properties:Settings.Default}" Path="Language" /&gt;
              &lt;Binding Path="Keyboard" /&gt;
          &lt;/MultiBinding&gt;
      &lt;/ContentControl.Content&gt;
  &lt;/controls:KeyboardHost&gt;
  </code></pre>
  
  <p><strong>KeyboardCriteriaValueConverter:</strong></p>
  
  <p>Convert method only, ConvertBack throws NotImplementedException.</p>
  
  <p>Logic has been simplified to remove extra logic to check value types, count, etc.</p>
  
  <pre><code>public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  {
      return new KeyboardCriteria
      {
          Language = values[0], 
          Keyboard = values[1]
      };
  }
  </code></pre>
  
  <p><strong>KeyboardDataTemplateSelector:</strong></p>
  
  <pre><code>public class KeyboardDataTemplateSelector : DataTemplateSelector
  {
      //TEMPLATE PROPERTIES HERE - THESE ARE SET IN THE RESOURCE DEFINITION
  
      public override DataTemplate SelectTemplate(object item, DependencyObject container)
      {
          var criteria = item as KeyboardCriteria;
  
          //LOGIC TO RETURN THE APPROPRIATE KEYBOARD DATA TEMPLATE BASED ON THE criteria
      }
  }
  </code></pre>
  
  <p><em>Thanks for any insight you can provide.</em></p>