I have a treeview control that functions like a folder browser.
Because loading the entire folder structer from disk is taking a lot of time i'm trying to load only one level at a time.
So i have a function that adds nodes for all the folders in the current node.
I thought that the best method would be to run it on the BeforeExpand event of the treeview.
UpdateTreeView(TreeView.SelectedNode);
is not working because clicking the + sign to expand is not selecting the node also.
So how to find the node that is expanding.
The BeforeExpand event should work. It has a TreeViewCancelEventArgs which contains a Node property. It is essentially the node being expanded.
Related
Consider a large TreeView; some nodes and subnodes are expanded others are collapsed. The user adds a child node to a node and the program saves everything back to the underlying XML file. Then the TreeView rebuilds. The problem is that only the root node is expanded; all other nodes are collapsed.
Has anybody ever seen code (I don't think there's anything built into WPF that does this) that saves the state of the TreeView (which nodes are expanded) and then is able to restore it to this state after a save?
Any ideas?
Edit
Well, that link didn't really help. I'm thinking of creating an array and storing the state of isExpanded and then reading it back after the load.
One has to manage the treeview's states directly and note when a node is expanded or collapsed. Then on load or refresh, open / close the nodes as needed.
I am working on a windows form application, where I connect to the database and get a list of projects and sub-projects. I am able to show this as a treeview with checkboxes, that later an user can select to do further operations.
My problem is that, I can't set the Parent node state when one of the child nodes is selected. Here, I want some indication, if any of the child nodes is selected the parent node should fill in to show that something has been selected below that parent node.
Okay, meanwhile, I found answer to my problem. Not really wise but thanks to this Simple Tri-State TreeView control. code link here.
I will definitely try to build my own custom version of this. For now, this works fine.
I have a treeview that utilizes the StateImageList to implement "proper" tri-state checks in a tree. The problem however is the first layer of nodes that get added to the tree. By default the StateImageIndex on a new TreeNode is -1, and I have no way of really knowing that they have been added. Nodes that are child nodes I can catch on the BeforeExpand and set the StateImageIndex properly but I cant seem to figure out what to do about the Top Layer of nodes.
My current work around (that I really really hate) is I have a timer running in the background checking to see if the number of nodes in the treeview has changed, if it has I run through and find all the root nodes that have a StateImageIndex set to -1 and set them to 0. It works, but it seems like a terrible waste of resources. Any one have a better suggestion?
No event, you can't generate one either. The native control doesn't support an event because only code can add nodes, not the user. You can't generate one because you can't break into the TreeNodeCollection class. A simple workaround is to add a static CreateNode() factory method to your class with the expectation that the client code uses that to create a properly initialized node.
You ought to consider the DrawMode property so you can just draw the node the way you want it. HitTest() to detect clicks, ControlPaint.DrawCheckBox() to draw the box.
I would like to insert an image to a TreeView node label. I can't use the node's icon because it's already used for other purposes. The other solution would be the ability to have 2 icons per node.
I suggest to expand TreeNode by some Image SecondIcon property, add collection of them to your TreeView and then subscribe to TreeView.DrawNode event.
Here you can find some more hints and example: http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.drawnode.aspx.
Using visual studios 2010 and c# i have a treeview and i added images to each of the nodes. In retrospect i now want to remove the images that are associated with the children of the 3 main root nodes. How do i remove the images from the children of my 3 main root nodes but keep the images on the 3 root nodes?
I have tried opening the properties for the nodes and changed the ImageIndex and SelectedImageIndex to "none" for all of the children nodes and when i press ok, it still shows the default image for each child node.
Thanks
I'm not sure of it, but I believe that you can pass through every control inside the TreeView doing a foreach in [TreeView].Controls... In there you can search for [control].GetType() == typeof([type_of_the_image]), and do some logic to get just the main ones.
Besides that, you can remove by jQuery, doing something close to what I said above. If these ideas are something that interests you, I can help with the development.