title: Access CustomControl's element programmatically in WPF
body: <p>I have created a custom control which will have only one Grid into it.</p>
  
  <p>Here is the part of the code from <code>Generic.xaml</code></p>
  
  <pre><code>&lt;Style TargetType="{x:Type local:MainView}"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type local:MainView }"&gt;
                      &lt;Grid x:Name="**PART_MyGrid**" Background="Black" Width="{TemplateBinding Width}"
                Height="{TemplateBinding Height}"&gt;
                          &lt;ContentPresenter Content="{TemplateBinding Content}" /&gt;
                      &lt;/Grid&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
   &lt;/Style&gt;
  </code></pre>
  
  <p>Corresponding <code>MainView.cs</code> is as follows: </p>
  
  <pre><code>[TemplatePart(Name = "PART_MyGrid", Type = typeof(Grid))]
      public class MainView : ContentControl
      {
          private Grid MainViewGrid;
          static MainView()
          {
              DefaultStyleKeyProperty.OverrideMetadata(typeof(MainView), new FrameworkPropertyMetadata(typeof(MainView)));
          }
          public override void OnApplyTemplate()
          {
              //This Function never gets called
              base.OnApplyTemplate();
  
              //Find the grid in the template once it's applied
              MainViewGrid = base.Template.FindName("**PART_MyGrid**", this) as Grid;
              //We can subscribe to its events
          }
          public void setGrid(DataGrid dtGrid)
          {
  ***//Exception saying MainViewGrid is null***
              MainViewGrid.Children.Add(dtGrid);
          }
      }
  </code></pre>
  
  <p>Now I have created a another project and I want to include this custom control into one of the panel programmatically.</p>
  
  <p>This is what I have done in .cs file from different project , where I wanted to create this CustomControl dynamically.</p>
  
  <pre><code>CustomControlLib.MainView m_View = new CustomControlLib.MainView();
  
  ***//... Code to create One Datagrid programmatically ...***
     m_View.setGrid(programmatically_created_dataGrid);
     theTabItem.Content = m_View;
     theTabItem.DataContext = m_View.DataContext;
  </code></pre>
  
  <p>What exactly I want is , I want to create <code>CustomControl</code> dynamically and then add it to <code>TabItem</code>.
  So, I want to access <code>Grid</code> in <code>CustomControl</code> and add <code>DataGrid</code> to it programmatically.
  But <code>OnApplyTemplate()</code> gets called only when the Custom control is displayed in the screen.
  In my case its giving exception saying <code>"MainViewGrid is null"</code>
  So, how do I access MainView CustomControl's element in this case or rather call <code>OnApplyTemplate()</code>, so that I can "find" Grid and add DataGrid to it.</p>