title: WPF: initial visibility and template loading
body: <p>I have encountered an issue where the bindings are not properly set on one of my user controls if that control is not visible when it is initialized. I have duplicated the issue with the following dumbed-down controls:</p>
  
  <pre><code>public class Test3 : Control
      {
          static Test3()
          {
              DefaultStyleKeyProperty.OverrideMetadata(typeof(Test3), new FrameworkPropertyMetadata(typeof(Test3)));
          }
  
          public string Test
          {
              get { return (string)GetValue(TestProperty); }
              set { SetValue(TestProperty, value); }
          }
  
          public static readonly DependencyProperty TestProperty =
              DependencyProperty.Register("Test", typeof(string), 
              typeof(Test3), new UIPropertyMetadata("test3 default text"));        
      }  
  
  public class Test2 : Control
      {
          static Test2()
          {
              DefaultStyleKeyProperty.OverrideMetadata(typeof(Test2), new FrameworkPropertyMetadata(typeof(Test2)));
          }
  
          public FrameworkElement Test3Control
          {
              get { return (FrameworkElement)GetValue(Test3ControlProperty); }
              set { SetValue(Test3ControlProperty, value); }
          }
  
          public static readonly DependencyProperty Test3ControlProperty =
              DependencyProperty.Register("Test3Control", typeof(FrameworkElement), 
              typeof(Test2), new UIPropertyMetadata(null));
  
          public string Test
          {
              get { return (string)GetValue(TestProperty); }
              set { SetValue(TestProperty, value); }
          }
  
          public static readonly DependencyProperty TestProperty =
              DependencyProperty.Register("Test", typeof(string), typeof(Test2), 
              new UIPropertyMetadata("test2 default text"));
      }
  
  public partial class Test1 : UserControl
      {
          public Test1()
          {
              InitializeComponent();
          }
  
          public string Test
          {
              get { return (string)GetValue(TestProperty); }
              set { SetValue(TestProperty, value); }
          }
  
          public static readonly DependencyProperty TestProperty =
              DependencyProperty.Register("Test", typeof(string), 
              typeof(Test1), new UIPropertyMetadata("test1 default text"));        
      }
  </code></pre>
  
  <p>XAML for usercontrol:</p>
  
  <pre><code>&lt;UserControl x:Class="Test.Test1"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:dummy="clr-namespace:WpfTestApplication.Test"
               Name="ucThis"&gt;
  
      &lt;UserControl.Resources&gt;
          &lt;Style TargetType="{x:Type test:Test2}"&gt;
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="{x:Type test:Test2}"&gt;
                          &lt;GroupBox Header="Test2"&gt;
                              &lt;StackPanel&gt;
                                  &lt;TextBox IsEnabled="False" Text="{TemplateBinding Test}"/&gt;
                                  &lt;GroupBox Header="Test3"&gt;
                                      &lt;ContentPresenter Content="{TemplateBinding Test3Control}"/&gt;
                                  &lt;/GroupBox&gt;
                              &lt;/StackPanel&gt;
                          &lt;/GroupBox&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
      &lt;/UserControl.Resources&gt;
  
      &lt;Grid&gt;
          &lt;test:Test2 Test="{Binding ElementName=ucThis,Path=Test}"&gt;
              &lt;test:Test2.Test3Control&gt;
                  &lt;test:Test3 Test="{Binding ElementName=ucThis,Path=Test}"&gt;
                      &lt;test:Test3.Template&gt;
                          &lt;ControlTemplate TargetType="{x:Type test:Test3}"&gt;
                              &lt;TextBox IsEnabled="False" Text="{TemplateBinding Test}"/&gt;
                          &lt;/ControlTemplate&gt;
                      &lt;/test:Test3.Template&gt;
                  &lt;/test:Test3&gt;
              &lt;/test:Test2.Test3Control&gt;
          &lt;/test:Test2&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>... and XAML for main window (the guts of it, anyway):</p>
  
  <pre><code>&lt;DockPanel&gt;            
      &lt;StackPanel&gt;
          &lt;TextBox Name="tbInput"/&gt;    
  
          &lt;Expander Header="Initially Visible" IsExpanded="True"&gt;
              &lt;test:Test1 Test="{Binding ElementName=tbInput, Path=Text}" /&gt;
          &lt;/Expander&gt;
  
          &lt;Expander Header="Initially Collapsed" IsExpanded="False"&gt;
              &lt;test:Test1 Test="{Binding ElementName=tbInput, Path=Text}" /&gt;
          &lt;/Expander&gt;
      &lt;/StackPanel&gt;    
  &lt;/DockPanel&gt;
  </code></pre>
  
  <p>I would expect that whatever text is entered into the textbox ("tbInput") would be displayed in the Test2 and Test3 boxes - and indeed it is for both instances of Test2 but only for the Test3 that is initially visible. The Test3 that is initially collapsed always displays the default text, even if it is visible when the text is entered.</p>
  
  <p>I've tried to investigate this using Snoop, but the issue corrects itself when I evaluate the relevant parts of the tree w/ Snoop so it hasn't been much help.</p>
  
  <p>What is causing this behavior? How can I correct it? Where can I read more about it?</p>
  
  <p>UPDATE:</p>
  
  <p>By watching the output window I discovered this error message:
  <code>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=ucThis'. BindingExpression:Path=Test; DataItem=null; target element is 'Dummy3' (Name=''); target property is 'Test' (type 'String')</code></p>
  
  <p>By handling the loaded and initialized events on these controls I can see that this error occurs after Dummy1 and Dummy2 have loaded at startup. Dummy 3 doesn't load until it is made visible.</p>