Using some custom style with a StyleSelector for my TreeView, I found a really strange bug, more related to ItemContainerGenerator, I think. I had to implement some drag and drop, and it appears that switching (my removing and re-inserting them) two sub-items in a sub-collection of a previously switched item in the parent collection used as ItemsSource for my TreeView lead to some unexpected error :
The container passed to StyleSelector.SelectStyle while reinserting the sub-items seems "invalid" ! It isn't related to the TreeView anymore : it isn't able to find ressources and if you try getting its parent ItemsControl, it is now null !
View the sample application I made to test this bug, since explaining here doesn't make the problem very clear : click on the Test button and an exception will occur while re-inserting sub-items. The code is very simple and is a basic data-binded TreeView. Of course this sample is basic, and the style selector is useless since there is only one style here, but that is not the case in a real application. Also note that you can wait indefinitely between the two collections modifications, the problem will still occurs.

Possible odd TreeView bug (sample included)
Debboy
Zhou Yong's answer isn't quite right. What's really happening is that the TreeViewItem corresponding to your MainItem1 gets removed from the tree when you remove MainItem1 from the main collection, but this TreeViewItem continues to listen to events from its subcollection. When you re-insert an item into this collection (mainItem1.Items.Insert(0, subItem1)), the old TreeViewItem hears the event and generates a new container (TreeViewItem) for it. This calls into your code to get a style, but your code can't find the resource because the container is no longer connected to the main tree.
I recommend using TryFindResource instead of FindResource, and returning null if the resource cannot be found.
Meanwhile, I'm sure your next question is "why does a TreeViewItem that has been removed from the tree continue to listen to events, generate containers for items, and behave as if it were still attached " The answer is that WPF tries to do as little as possible when a container is removed -- this helps the perf of scrolling a UI-virtualized list where removing containers from the tree is a common operation. Normally those containers will be GC'd pretty soon, so there's no point in wasting cycles to clean them up. However in this case, it looks as if WPF has done too little. I will see that a bug is opened about this.
srjing2
EisenB
Duh. I don't need tools - just need to look at the repro with my eyes open.
The explanation is simple. The repro adds new items to mainItem1, but not to mainItem0. Both of these get removed from the tree, but that doesn't cause any harm until you add sub-items. At which point we generated a new "sub-item" container underneath a disconnected "item" container. (BTW, it's mainItem1 that has the problem, and mainItem0 that doesn't - not the other way around.)
amirkanan
Shark_
Farhan H Soomro
Sheva
Lariamon
}Sheva
Tryst
Edit: just a quick question, It seems that only TreeViewItem for mainItem0 causes this problem, TreeViewItem for mainItem1 is fine here, I think both of those TreeViewItems gets removed here, Could you please give some explanation to it here
Sheva
SOTY_Programmer
yoshikatsu
Sheva
Tim Cools