I am trying to check whether a tree node is expanded or not, but unfortunately, it appears that the TreeNode. Expanded field is always null until the user either expands or collapses a node. However, as the Tree View starts out with only one level expanded (there's too much information for all levels to be expanded at the beginning), this is not accurate and cannot be solved by simply checking for null or expanded (which is what I had assumed would work at first). Any ideas on what to do?
-
Check if it is null or open?krillgar– krillgar2017-08-11 13:28:15 +00:00Commented Aug 11, 2017 at 13:28
-
Have you checked to see if it is null when collapsed or expanded? If the user hasn't interacted with it, why care? What are your goals here?user1228– user12282017-08-11 13:28:49 +00:00Commented Aug 11, 2017 at 13:28
-
@Will The tree starts out not fully expanded, but the nodes don't seem to register that.Reed Oei– Reed Oei2017-08-11 13:32:25 +00:00Commented Aug 11, 2017 at 13:32
Add a comment
|
1 Answer
I solved the problem by programmatically collapsing all the nodes in the tree, then re-expanding the ones I wanted expanded using the method below:
private void ExpandTree(int Depth)
{
tr.CollapseAll();
foreach (TreeNode tn in tr.AllNodes().Where(tn => tn.Depth < Depth))
{
tn.Expand();
}
}
AllNodes() is an extension method that gets all nodes in the tree view, breadth-first.