So there's a sub genre of questions about WPF which ask about why ItemContainerGenerator.ContainerFromItem
returns null. This can be for a multitude of reasons, and changes based on the control this is being called on.
A while ago, while trying to solve this problem for a TreeView
, I came across this answer which solved my problem. Note that it is posted on a question about ListBox
, and the solution to that particular problem turned out to be quite different. I suggested that the answerer post it on a different question where it actually answers the question being asked, but they haven't responded.
So, I decided to go looking for related questions to see if this has been asked before about TreeView
s. It certainly has. Here is a selection of the questions I came across.
- WPF cannot get ItemContainerGenerator.ContainerFromItem to work
- ContainerFromItem at TreeView returns null
- Treeview ContainerFromItem always returns null
The only question that would fit this answer seems to be the last one, where the accepted answer actually suggests a method like the one in the linked answer, but without any code. I guess that this would be the appropriate place to put this answer, but I hesitate to do so as I want credit to go to the original answerer.
So, what's the best course of action here? Leave it be? Add the answer to another question as a community wiki? Perhaps we even need a canonical version of this question, to explain the different reasons ItemContainerGenerator.ContainerFromItem
can return null?
If none of these apply, how can we make it easy for people to find the version of the question they need? I'm coming at this from the angle that we should make it as simple as possible for people to find the answers they need. This is what the duplicate system is for, but none of these seem to be direct duplicates. Perhaps a canonical question would be able to address everything and be a dupe target for all the rest.
Related note: Finding these questions with Google is actually quite difficult, as the results change completely depending on your search terms. If you don't have exactly the right keywords, you're likely to come across one of the questions which is different to the problem you're having. That's actually how I came across the linked answer in the first place.