title: Specify .XAML part of UserControl in code-behind
body: <p>I wrote custom <code>TreeView</code> control.</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;TreeView x:Class="EArchiveMaster.View.MyTreeView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               mc:Ignorable="d" 
               d:DesignHeight="300" d:DesignWidth="300"&gt;
      &lt;TreeView.ItemContainerStyle&gt;
          &lt;Style TargetType="{x:Type TreeViewItem}"&gt;
              &lt;Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /&gt;
              &lt;Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /&gt;
              &lt;EventSetter Event="LostFocus" Handler="EventSetter_OnHandler" /&gt;
          &lt;/Style&gt;
      &lt;/TreeView.ItemContainerStyle&gt;
  &lt;/TreeView&gt;
  </code></pre>
  
  <p>.cs</p>
  
  <pre><code>public partial class MyTreeView
  {
      public event Action SomeItemLostFocus;
  
      public MyTreeView()
      {
          InitializeComponent();
      }
  
      private void EventSetter_OnHandler(object sender, RoutedEventArgs e)
      {
          e.Handled = true;
          if (SomeItemLostFocus != null)
              SomeItemLostFocus();
      }
  }
  </code></pre>
  
  <p>But when I try to use it I got well known error:</p>
  
  <p><em>Cannot set Name attribute value 'TextBox' on element 'TextBox'. 'TextBox' is under the scope of element 'MyTreeView', which already had a name registered when it was defined in another scope.</em> </p>
  
  <p>I found some receipts how to fix this error. Namely, specify .xaml part of control in its code-behind.
  But I have no idea how can I do this. </p>