Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is that the nested <code>ItemContainerGenerators</code> aren't generated all at the beginning, they are generated on-demand. And even more so, they are generated in a separate thread, so you have to listen to a <code>StatusChanged</code> on the generator to be sure it is ready =(</p> <p>Some people suggest to play with the <code>Dispatcher</code> (<a href="http://bea.stollnitz.com/blog/?p=59" rel="nofollow noreferrer">like in this Bea's post</a>). I tried to implement the Dispatcher solution, but it didn't work for some reason... the generators are still empty =(</p> <p>So I ended up with another one, where you specifically ask the tree to update its layout, which causes the generation for the expanded nodes. Here's the final method... you might want to test it a little to verify that it suites your needs. It might collapse some nodes that were expanded before its run.</p> <pre><code> private static bool SetSelected(TreeView treeView, ItemsControl parentControl, INestable itemToSelect) { if (parentControl == null || itemToSelect == null) { return false; } foreach (INestable item in parentControl.Items) { TreeViewItem container = parentControl.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem; if (item.ID == itemToSelect.ID) { // just comparing instances failed container.IsSelected = true; container.Focus(); return true; } container.IsExpanded = true; treeView.UpdateLayout(); WaitForPriority(DispatcherPriority.Background); if (SetSelected(treeView, container, itemToSelect)) return true; else container.IsExpanded = false; } return false; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload