title: How to make TreeView node selected/expanded when ItemsSource = custom object collection?
body: <p>I have a <code>TreeView</code> intended to operate on two levels. For his I have two <code>HierarchicalDataTemplate</code> and two custom types. The <code>ItemsSource</code> is linked to a <code>ObservableCollection</code> and everything works fine. I just can't figure how to make a node selected or expanded from codebehind. Somewhere was mentioned a very good idea of binding <code>IsExpanded</code> and <code>IsSelected</code> properties to the corresponding properties in my custom types. The only problem is that the <code>HierarchicalDataTemplate</code> does not implement a <code>TreeViewItem</code> directly, so how can I access these properties in the following code?</p>
  
  <pre><code>&lt;TreeView Name="treeViewNotes" AllowDrop="True" PreviewMouseLeftButtonDown="treeViewNotes_PreviewMouseLeftButtonDown" PreviewMouseMove="treeViewNotes_PreviewMouseMove" Drop="treeViewNotes_Drop" DragEnter="treeViewNotes_DragEnter"&gt;
      &lt;TreeView.Resources&gt;
          &lt;HierarchicalDataTemplate DataType="{x:Type dataclasses:NoteFolder}" ItemsSource="{Binding Notes}"&gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;Image Height="16" Source="{Binding TreeViewIcon}" Tag="{Binding Self}"/&gt;
                  &lt;TextBlock Text="{Binding Title}" Tag="{Binding Self}" Margin="3"/&gt;
              &lt;/StackPanel&gt;
          &lt;/HierarchicalDataTemplate&gt;
          &lt;HierarchicalDataTemplate DataType="{x:Type dataclasses:Note}"&gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;Image Height="16" Source="{Binding TreeViewIcon}" Tag="{Binding Self}"/&gt;
                  &lt;TextBlock Text="{Binding Title}" Tag="{Binding Self}" Margin="3"/&gt;
              &lt;/StackPanel&gt;
          &lt;/HierarchicalDataTemplate&gt;
      &lt;/TreeView.Resources&gt;
  &lt;/TreeView&gt;
  </code></pre>
  
  <p>My goal is when creating and adding a new <code>Note</code> to some <code>NoteFolder</code>, to make this <code>Note</code> selected and the <code>Folder</code> expanded. The same would be needed to further improve the UI response on drag&amp;drop.</p>