title: Why doesnt Window.FindName() discover the x:Name of a button in a child UserControl? AKA how do NameScopes work?
body: <p>So in the example code below, I create a UserControl UserControldChild which is a child of the main Window, Window1.xaml. Why does the <code>FindName()</code> method fail to find the "myButton" in the code below? </p>
  
  <p>This must have to do with the <a href="http://msdn.microsoft.com/en-us/library/ms746659.aspx" rel="nofollow noreferrer">WPF XAML NameScopes</a>, but I have yet to find a good explanation as to how NameScope works. Can someone enlighten me? </p>
  
  <pre><code>//(xml) Window1.xaml    
  &lt;Window x:Class="VisualTreeTestApplication.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:VisualTreeTestApp="clr-namespace:VisualTreeTestApplication"
      Title="Window1" Height="400" Width="400"&gt;
      &lt;Grid&gt;
          &lt;VisualTreeTestApp:UserControlChild/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  
  //(c#) Window1.xaml.cs
  namespace VisualTreeTestApplication
  {
    /// &lt;summary&gt;
    /// Interaction logic for Window1.xaml
    /// &lt;/summary&gt;
    public partial class Window1 : Window
    {
      public Window1()
      {
        InitializeComponent();
        Button btnTest = (Button)Application.Current.MainWindow.FindName("myButton");
        // btnTest is null!
      }
    }
  }
  </code></pre>
  
  <p>UserControl below:</p>
  
  <pre><code>//(wpf) UserControlChild.xaml
  &lt;UserControl x:Class="VisualTreeTestApplication.UserControlChild"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Height="300" Width="300"&gt;
      &lt;Grid x:Name="myGrid"&gt;      
          &lt;Button x:Name="myButton" Margin="20" &gt;Button&lt;/Button&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  
  //(c#) UserControlChild.xaml.cs (no changes)
  namespace VisualTreeTestApplication
  {
    /// &lt;summary&gt;
    /// Interaction logic for UserControlChild.xaml
    /// &lt;/summary&gt;
    public partial class UserControlChild : UserControl
    {
      public UserControlChild()
      {
        InitializeComponent();
      }
    }
  }
  </code></pre>
  
  <p>In case this doesn't get answered properly, I found an alternative to using FindName() documented <a href="https://stackoverflow.com/questions/636383/wpf-ways-to-find-controls/1759923#1759923">in the post here</a>. </p>