title: WPF Treeview Expand only a first node and selected item?
body: <p>I use a treeview in WPF and I don't want to lose the state (expanding and focus) when I reload the binded collection.</p>
  <p>At the first load it's easy to expand just the first node, i use the following code :</p>
  <pre><code>    private void ExpandFirstNodeTree()
      {
          foreach (var item in TreeviewModel.Items)
          {
              TreeViewItem itm = (TreeViewItem)TreeviewModel.ItemContainerGenerator.ContainerFromItem(item);
              if (itm == null) continue;
              itm.IsExpanded = true;
          }
      }
  </code></pre>
  <p>I use a DependencyProprety to select an item. I explore the TreeView, find the TreeViewItem and set the item &quot;IsSelected&quot; property to true.</p>
  <pre><code>private static readonly DependencyProperty SelectedEntityCodeProperty =
          DependencyProperty.Register(PropertyHelper.GetName((EntitiesTreeview e) =&gt; e.SelectedEntityCode), typeof (string), typeof (EntitiesTreeview));
  
      public string SelectedEntityCode
      {
          get { return (string) GetValue(SelectedEntityCodeProperty); }
          set { SetValue(SelectedEntityCodeProperty, value); }
      }
  
      public EntitiesTreeview()
      {
          InitializeComponent();
          Loaded += new RoutedEventHandler(EntitiesTreeview_Loaded);
      }
  
      private void LoadSelectedItem()
      {
          if ((!string.IsNullOrEmpty(SelectedEntityCode))
              &amp;&amp; (TreeviewEntity.SelectedItem == null))
              ChangeSelectedItem&lt;ENTITY&gt;(SelectedEntityCode, TreeviewEntity);
      }
  
      private bool ChangeSelectedItem&lt;T&gt;(string entityCode, ItemsControl itemsControl) where T : ENTITYBASE
      {
          if (itemsControl != null)
          {
              foreach (var item in itemsControl.Items)
              {
                  var currentContainer = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
                  if ((currentContainer != null)
                      &amp;&amp; (item is T)
                      &amp;&amp; ((item as T).CCODE == entityCode))
                  {
                      currentContainer.IsSelected = true;
                      var selectMethod = typeof (TreeViewItem).GetMethod(&quot;Select&quot;, BindingFlags.NonPublic | BindingFlags.Instance);
                      selectMethod.Invoke(currentContainer, new object[] {true});
                      return true;
                  }
                  if (ChangeSelectedItem&lt;T&gt;(entityCode, currentContainer))
                      return true;
              }
          }
          return false;
      }
  </code></pre>
  <p>My problem is, when I reload the items collection, the focus is lost (selected item) and expanded items are collapsed. How can I separate bound items and ui ?
  Last point, I want to set a selected item programmatically. How can I reload the selected item when the dependency property changed ?</p>
  <hr />
  <p>I've already had a look to the josh smith solution ( <a href="http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx</a> ) but I don't want to use a ViewModel collection for my binding. I have different object type to bind and use ViewModel will be to painfull... IMO :)</p>