Dynamic TreeView issue - need help
I am trying to dynamically build a TreeView. I want to be able to expand a top level node & have any child nodes be added on the fly. The problem I'm running into is that I can't get the "+" symbol to appear if there are no subnodes to begin with. I could probably add a "dummy" subnode, then when the parent node is expanded I could delete the dummy node & add my real nodes, but that sounds cheesy. Can someone please explain the proper way to do this or point me to some code? Thanks...
Re: Dynamic TreeView issue - need help
Hmm... I think the simple answer is there isn't any way other than your "cheesy" way (which I don't think is cheesy by the way - it is used quite commonly).
The "+" is there to signify the presence of child nodes. If there are no child nodes there is no "+", simple as that.
The only other thing you may be able to do is make the treeview "owner-draw" (make the treeview's DrawMode = OwnerDrawAll) and you would be responsible for painting the content of the treeview yourself, which is a lot more hassle than doing it by creating dummy nodes.
Re: Dynamic TreeView issue - need help
This is something I've been intending to investigate for a while because I'd like to be able to do the same thing. I'm guessing that it's possible to inherit the TreeNode and/or TreeView classes and make this happen, although it might take some messing with unmanaged code. I'll schedule it for investigation this weekend and let you know how I get on.
Re: Dynamic TreeView issue - need help
Please do let us know what you find out. My "cheesy" way does work, but I'm sure anything you come up with will be far more elegant.
Re: Dynamic TreeView issue - need help
I have to say I have always used the way you described. I just add a node that says "Loading..." or something like that so that if somehow the user does see it for a second before the proper nodes are added then thats all they see until the dynamic nodes are added. I dont really see anything wrong with this method but I would be interested to see if it could be done more 'properly'
Re: Dynamic TreeView issue - need help
You don't even need to see a temporary node at all if you populate your branch during the node BeforeExpand event.
Re: Dynamic TreeView issue - need help
Ah I see, never tried that but I will bear it in mind if I ever need to do that again ;)
PS it took me about 30 seconds to make this work the way you want to in WPF. Its the future, I'm telling you!
Re: Dynamic TreeView issue - need help
Quote:
Originally Posted by
chris128
PS it took me about 30 seconds to make this work the way you want to in WPF. Its the future, I'm telling you!
I think it's a bit too early to say that right now. WPF could take off like a rocket or it could quitely die off in a few years just like many other technology innovations.
Re: Dynamic TreeView issue - need help
You dont think the fact that the new version of the primary application that Windows developers use (Visual Studio) is made in WPF shows that it may be slightly more serious than "just another technology" ? I'm certainly not saying it is perfect and it needs to mature quite a lot still but I was just commenting on how easy it makes things like this...
Anyway I guess this is not really a discussion to be had in this thread :)
Re: Dynamic TreeView issue - need help
How would you want the TreeView to react to the following situation:
You have a TreeNode that has no children and you select it, the [+] box goes away. Then you navigate away from that node (by selecting a different one). Should the Node redraw that [+] OR should it stay cleared? Because potentially nodes could get dynamically added to that node after you select a different one.
Re: Dynamic TreeView issue - need help
I think it should stay cleared. That's how I made mine behave.
Re: Dynamic TreeView issue - need help
Oh I thought the idea was that if the node did not have any children then it wouldnt get the plus symbol. I think it would look pretty naff having a plus symbol that when the user clicks it just disappears :S
Re: Dynamic TreeView issue - need help
In my case, there's no way to know ahead of time if a node will have children or not, so I just always put the "+" there. If the user expands it & there are no child nodes, then it clears.
Re: Dynamic TreeView issue - need help
Oh right, would have taken even less time in WPF if I had known that :D (sorry stanav :))
Re: Dynamic TreeView issue - need help
JMC ... just wondering if you had a chance to investigate this...
Re: Dynamic TreeView issue - need help
Quote:
Originally Posted by
nbrege
JMC ... just wondering if you had a chance to investigate this...
I had a look through the TreeView code in Reflector and didn't see anything that appeared useful. I'll take a closer look at the Win32 control some time soon.