title: WPF TreeView items updating - container retrieval
body: <p>Bit of a strange one.</p>
  
  <p>I have a Treeview control that is being populated with a list of Customer objects. There is a search feature to find a certain object somewhere in the tree which has a line of code as follows.</p>
  
  <pre><code>foreach (RbcUICustomer cust in tvQueue.Items)
  {
      var dataTVI = tvQueue.ItemContainerGenerator.ContainerFromItem(cust) as TreeViewItem;
  </code></pre>
  
  <p>The tree starts closed and if you step through the code after performing a search, if the entire tree is closed, this returns a valid TreeViewItem for each customer node in the tree. Which is fine, and what you would expect.</p>
  
  <p>The problem is if you manually open one of the nodes first, and THEN perform the search, the search loop above gets to the expanded node and the ItemContainerGenerator method above returns null, so it does not find the container for the item. </p>
  
  <p>I assume that the problem is something to do with what I am doing to the underlying data when you expand the node manually, which in some way alters it and doesn't refresh it properly. The business idea behind expanding a node is that it fetches a list of customer accounts for the customer from the service layer and adds them visually to the tree programmatically and expands the node out. But by doing this, it seems to then prevent the above line of code from being able to find and return the valid container. </p>
  
  <p>The code to update the customer is a bit convoluted but I will paste the essence below. _customerList is the class array used to hold the data.</p>
  
  <pre><code>ExpandNode(TreeViewItem item)
  {
      var cust = item.Header as Customer;
      var task = new Task(() =&gt; RunBackgroundThreadToAddAccounts(Customer));
      task.Start();
  }
  
  RunBackgroundThreadToAddAccoutns(Customer)
  {
      for (var i = 0; i &lt; _customerList.Count; i++)
                  {
                      var customer = _customerList[i];
  
                      if (customer.CustomerId == cust.CustomerId)
                      {
                          customer.Accounts.Clear();
                          customer.Accounts.Add(LoadingAccount);
  
                          _customerList[i] =  customer;                                   
                          UpdateCustomerCompleted(item, i);
                          break;
                      }
                  }
  }
  
   private void UpdateCustomerCompleted(TreeViewItem item, int positionIndex)
          {
              var customer = _customerList[positionIndex];
  
              GUIHelper.Invoke(() =&gt; item.Header = customer);
  
              if (customer.Accounts.Count == 0)
              {
                  customer.Accounts.Add(EmptyAccount);
              }
  
              GUIHelper.Invoke(() =&gt; item.ItemsSource = customer.Accounts);
              GUIHelper.Invoke(() =&gt; _treeSelectionHandler.ChangeSelectedState(item));
              GUIHelper.Invoke(() =&gt; item.Items.Refresh());
              GUIHelper.Invoke(item.UpdateLayout);
          }
  </code></pre>
  
  <p>Can anyone suggest what about the above code to Expand the node might be invalidating the tree data in such a way that when I try and retrieve the container for it, it returns null?</p>