title: Custom control OnApplyTemplate called after dependency property callback
body: <p>I'm developing my first WPF custom control and I'm facing some problems, here's a simplified version of the code I'm currently using:</p>
  
  <pre><code>using System.Windows;
  using System.Windows.Controls;
  
  namespace MyControls
  {
      [TemplatePart(Name = "PART_Button", Type = typeof (Button))]
      public class MyControl : Control
      {
          public static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof (object), typeof (MyControl), new PropertyMetadata(null, OnLabelPropertyChanged));
  
          private Button _buttonElement;
  
          public object Content
          {
              get { return this.GetValue(LabelProperty); }
              set { this.SetValue(ContentProperty, value); }
          }
  
          static MyControl()
          {
              DefaultStyleKeyProperty.OverrideMetadata(typeof (MyControl), new FrameworkPropertyMetadata(typeof (MyControl)));
          }
  
          private static void OnContentPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
          {
              MyControl myControl = sender as MyControl;
              if (myControl != null &amp;&amp; myControl._buttonElement != null)
                  myControl._buttonElement.Content = e.NewValue;
          }
  
          public override void OnApplyTemplate()
          {
              base.OnApplyTemplate();
  
              this._buttonElement = this.Template.FindName("PART_Button", this) as Button;
          }
      }
  }
  </code></pre>
  
  <p>This is the template for my custom control:</p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:local="clr-namespace:MyControls"&gt;
      &lt;Style TargetType="{x:Type local:MyControl}"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type local:MyControl}"&gt;
                      &lt;Button x:Name="PART_Button" /&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>Then i put it inside a Grid and try to set its Content property:</p>
  
  <pre><code>&lt;Grid x:Name="layoutRoot"&gt;
      &lt;controls:MyControl x:Name="myControl" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>Here's the code behind:</p>
  
  <pre><code>using System.Windows;
  
  namespace MyControls
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              this.InitializeComponent();
  
              this.myControl.Content = "test";
          }
      }
  }
  </code></pre>
  
  <p>This doesn't work, for some reason the OnContentPropertyChanged callback is called before OnApplyTemplate, so myControl._buttonElement is assigned too late and it's still null when trying to set its content. Why is this happening and how can I change this behavior?</p>
  
  <p>I also need to provide full design time support but I cannot find a way to make my custom control accept some additional markup, much like the Grid control does with ColumnDefinitions:</p>
  
  <pre><code>&lt;Grid x:Name="layoutRoot"&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition /&gt;
          &lt;ColumnDefinition /&gt;
      &lt;/Grid.ColumnDefinitions&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I found a document that explains why the OnApplyTemplate method is called after control properies are set:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/dd351483%28v=vs.95%29.aspx">http://msdn.microsoft.com/en-us/library/dd351483%28v=vs.95%29.aspx</a></p>
  
  <p>So the question is: how can I keep track of the properties that are set (in XAML or programmatically) and methods that are called when the control has not been initialized, so that I can set/call them when the OnApplyTemplate method is called? How can the same callback/method work both before and after the control initialization without duplicating the code?</p>