How to get child location in a treeview? - c#

I currently have a simple tree view that contains one parent node with multiple child nodes. I was wondering if there is a way to find the location of the selected node in the parent tree.
I currently have an action event on the treeview and when the user clicks on the child it prints out the string value of the selected child. I have tried using:
int val = TreeView.SelectedItemProperty.GlobalIndex;
but it always returns 0. I have seen some examples in VB but I cant seem to get the same idea to work in C#.

You have to use the ItemContainerGenerator property of the Treeview.
http://msdn.microsoft.com/en-us/library/system.windows.controls.itemcontainergenerator.aspx
See: ContainerFromIndex and IndexFromContainer
Note that each TreeViewItem also has an ItemContainerGenerator (its an ItemsControl), so you'd have to recursively search down the tree if you have multiple levels.

I think the answer to all your treeview problems (and most ui ones) in wpf is to build a ViewModel. Anytime you start crawling the visual tree to look for elements that you are already binding to, you are doing things the hard way. Once you start using ItemsContainerGenerator you have to start worrying about a whole lot of issues you should not have to.
You are binding to a hierarchical structure. If that structure has a selected item property on each item and it is bound to the TreeViewItem selected item then you can just get the selected item in code and do everything else from there. Have a look at a similiar question here.

So i didn't find the answer i was looking for (I may of confused others with what my question was. by saying location). Anyways how I solved it was I got the string value of the child selected and compared it to my list. Thanks to those who answered!

Related

WPF: Selecting an item in (virtualized) TreeView

I have a virtualized TreeView and am successfully using the mechanism described here to select a given domain object in the view (expanding the path to the object accordingly), but the tree is always realized fully, which is prohibitively slow for my case (several thousand items).
Is there a better way of getting from the object in a domain hierarchy to the tree
item instead of this brute force method? I know the path to the item in the domain hierarchy, so I would like to realize only those items that lie on the path to the item to be selected, but I have not found out how to do so.
A method I've used in the past is to also include an IsExpanded property binding to a ViewModel object (which represents your visual TreeItem).
You can then walk the path from your leaf node back to the root of the tree and setting IsExpanded to true as you go which means no brute force nasty non-MVVM compliant code in the code behind of your XAML to do so.

How to set threeview node checkbox to mix state i.e. if any of the child node is selected, Parent node should fill in to indicate that

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.

Updating treeview control in WPF

In my application as soon as an object is dropped on canvas I stored it in a tree. Now, I'm trying to write a method to delete an object which is fine but I also need to delete that specific object from the tree. Each object has a unique id so this way I can find it in tree and remove it from tree while being deleted from the canvas.
In order to find a node in tree I have planned to store ID in each node (item.Tag), however, I'm facing two problems:
How can I access to details of a node from a different class? myTreeObj.Items.Tag doesn't work properly.
When I loop in tree myTreeObj.Items.Count shows more items that what I see.
Any comments will be appreciated.
Thanks.
In WPF, TreeViews are actually what they say they are: a view of a data structure. In WinForms, you had to crawl up and down the nodes of a TreeView and add them or remove them manually. In WPF, the proper approach is to add or remove items from the actual data hierarchy (to which the TreeView is binding) and use PropertyChanged or CollectionChanged notifications to tell the TreeView to update itself automatically.
What you are trying to do may be possible, but it is going to be an uphill fight all the way, and things will not work as expected. WPF REALLY wants you to use databinding, and any other approach is going to give you headaches.
This article may be a helpful place to start understanding how to work with the WPF TreeView:
http://joshsmithonwpf.wordpress.com/2008/05/24/the-wpf-treeview-is-a-view-of-a-tree/
EDIT:
The author's original article is actually more helpful:
http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode

Limit how deep TreeView would go when expanding TreeNode in WPF

I'm using WPF TreeView bound to my classes though MVVM to display a large array of multilevel data. Because of this, when I expand TreeNode, it takes some time and a lot of memory.
I think, that the memory requirement comes from TreeView scouting out nodes down the line, not just the first generation of the children, forcing those nodes to load data.
Is there any way to limit this "scouting" behavior to only the first generation of child nodes?
WPF generates TreeViewItem(s) only when they are displayed, i.e. no item is generated until its Parent expanded. So, I think the problem is not a "scouting".
Anyway, you may try to implement dynamic loading in your ViewModel by adding synchronization with TreeViewItem.IsExpanded using TwoWay Binding. Take a look at this. You may use the same solution for IsExpanded property. Don't forget to add fake item to tell TreeViewItem that it is expandable.
Check if TreeView is using VirtualizingStackPanel. I believe that it is used by default in .NET 4.0, but you must set it explicitly in previous .NET versions.
For more information, see How to: Improve the Performance of a TreeView.

TreeView Databind - from c# object?

I would like to databind my TreeView from a data structure. After googling some, I came to a conclusion that writing my own simple Tree structure using List of Nodes would be the thing to do. However, I would like to know whether I am on the right path and not overdoing it - from what I know there is no Tree structure in c#.
What are the preferable methods to bind a TreeView to the data? (dynamic of course).
Can I do something like:
List<node> nodes = new List<Node>();
//populate the nodes object
myTree.DataSource = nodes;
that would be ideal....Has anyone had any luck binding the tree to an actual object?
From what i know so far, i would have to manually add TreeNode nodes to the tree and populate them from my nodes list, which is fine, but if there is an easier way, that would be better.
Thanks!
MSDN has a good amount of information on binding to the TreeView.
I prefer using the SiteMap to bind data.
Also, you can create nodes through LINQ to XML
If you're using ASP.NET, you can read up on databinding for TreeViews here.
In winforms, however, TreeView doesn't support databinding. You have to do your own, so your understanding that you have to manually add nodes is correct.
You can implement INotifyPropertyChanged on your underlying data structure and then listen for that event and redraw the TreeView as needed if/when your nodes change.
Here is a good link that explain how to databind a treeview with the following functionality:
Load the data in the tree structure.
Keep track of changes in the data, showing them in the control
Expose a get/set property like SelectedValue besides SelectedNode, generally you have the value of the node you want to select (but not the node itself!!!)
DataBound TreeView

Categories

Resources