This is a migrated thread and some comments may be shown as answers.

NextItem

5 Answers 177 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Jamest
Top achievements
Rank 2
Jamest asked on 29 Apr 2009, 07:14 PM
I could have sworn I had this working with Q1. But with SP1 it seems to be not working. I am implementing a search on a tree (fully populated). .NextItem used to give the next node in the tree, now it only returns the next visible node. Is there a way to walk through the nodes (inorder traversal), without having to expand them all in the UI?

5 Answers, 1 is accepted

Sort by
0
Miroslav
Telerik team
answered on 30 Apr 2009, 11:58 AM

Hello Ender,

Unfortunately the NextItem method will only return TreeViewItems that have been created. If an item has never been expanded, it will not have been created. I am not certain if the behavior has changed for the SP, but this is the expected behavior.

If you want to act when an item has been created, you can register for the ItemPrepared event like so:

treeView.ItemPrepared += new EventHandler<RadTreeViewItemPreparedEventArgs>(treeView_ItemPrepared);  
 
void treeView_ItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)  
{  
    var treeViewItem = e.PreparedItem;  
 
    //Also this is how you get the item "depth":  
    var level = treeViewItem.Level  
}  
 

Also, setting properties on each TreeViewItem can happen via the ItemContainerStyle property. We can offer suggestions on how to avoid the traversal if possible.

All the best,

Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Jamest
Top achievements
Rank 2
answered on 30 Apr 2009, 12:33 PM

    public static RadTreeViewItem Find(RadTreeView pTVControl, string pSearch, bool newSearch)  
        {  
            try  
            {  
                if (pTVControl != null && pTVControl.Items.Count > 0)  
                {  
                    RadTreeViewItem currentItem = null;   
                    if (newSearch)  
                        currentItem = (RadTreeViewItem)pTVControl.Items[0];  
                    else  
                    {  
                        if (pTVControl.SelectedItem != null)  
                            currentItem = (RadTreeViewItem)pTVControl.SelectedItem;  
                        else  
                            currentItem = (RadTreeViewItem)pTVControl.Items[0];  
                           
                        currentItemcurrentItem = currentItem.NextItem;  
                        if (currentItem == null)  
                            return null;  
                    }  
                    do  
                    {  
                        MMNode mmn = (MMNode)currentItem.Tag;  
                        if (mmn.Name.ToLower().Contains(pSearch.ToLower()) || mmn.Description.ToLower().Contains(pSearch.ToLower()))  
                        {  
                            return currentItem;  
                        }   
                        currentItemcurrentItem = currentItem.NextItem;  
                    }  
                    while (currentItem != null);  
                }  
                return null;  
            }  
            catch (Exception ex)  
            {  
                WCF.LogError("MMTreeViewNodeHelper:Find", null, ex);  
                return null;  
            }  
        }  
    }  
 
        private void bnMyLibrarySearch_Click(object sender, RoutedEventArgs e)  
        {  
            if (!string.IsNullOrEmpty(this.txtMyLibrarySearch.Text))  
            {  
   
                RadTreeViewItem rtvi = MMTreeViewNodeHelper.Find(this.tvMyLibrary, this.txtMyLibrarySearch.Text, (bnMyLibrarySearch.Tag == null ? false : true));  
                if (rtvi == null)  
                {  
                    RadWindow.Alert(new DialogParameters() { Content = "Search has reached the end."Header = "End of search" });  
                    bnMyLibrarySearch.Tag = false;  
                }  
                else  
                {  
                    rtvi.EnsureVisible();  
                    rtvi.IsSelected = true;  
                    bnMyLibrarySearch.Tag = null;  
                }  
                txtMyLibrarySearch.Focus();  
            }  
            else  
            {  
                RadWindow.Alert("Please enter a value to search");  
            }  
        } 
        public static void BuildTree(RadTreeView pTVControl, XDocument pXDoc)  
        {  
            try  
            {  
                IEnumerable<XElement> elList = pXDoc.Root.Elements();  
                pTVControl.Items.Clear();  
                foreach (XElement xe in elList)  
                {  
                    RadTreeViewItem tn = new RadTreeViewItem();  
                    MMNode mmn = new MMNode(xe, WCF.cBaseDirectory);  
                    tn.Header = mmn.Name;  
                    tn.IsExpanded = mmn.IsExpanded;  
                    tn.DefaultImageSrc = mmn.TypeImage;  
                    tn.ExpandedImageSrc = mmn.TypeImageOpen;   
                    tn.Tag = mmn;  
                    if (xe.HasElements)  
                        AddNodes(xe, tn);  
                    pTVControl.Items.Add(tn);  
                }  
            }  
            catch (Exception ex)  
            {  
                WCF.LogError("MMTreeViewNodeHelper:BuildTree", null, ex);  
            }  
        }  
        private static void AddNodes(XElement xe, RadTreeViewItem tn)  
        {  
            try  
            {  
                IEnumerable<XElement> elList = xe.Elements();  
                foreach (XElement el in elList)  
                {  
                    RadTreeViewItem tn1 = new RadTreeViewItem();  
                    MMNode mmn = new MMNode(el, WCF.cBaseDirectory);  
                    tn1.Header = mmn.Name;  
                    tn1.IsExpanded = mmn.IsExpanded;  
                    tn1.DefaultImageSrc = mmn.TypeImage;  
                    tn1.ExpandedImageSrc = mmn.TypeImageOpen;   
                    tn1.Tag = mmn;  
                    if (el.HasElements)  
                        AddNodes(el, tn1);  
                    tn.Items.Add(tn1);  
                }  
            }  
            catch (Exception ex)  
            {  
                WCF.LogError("MMTreeViewNodeHelper:AddNodes", null, ex);  
            }  
        } 

Thank you for your response. I build the trees programmatically from custom XML from a WebServices and put my object (MMNode) in the tree node's tag. There is no LOD and MMNode has the properties I am searching on. .NextItem returns correctly if I ExpandAll() before calling search. It would be nice to only have the selected item visible/expanded out. rather than having to store the expanded states of every node and maintain them before and after every search. If you had any suggestions I would be happy to hear them.
Thanks again
0
Miroslav
Telerik team
answered on 01 May 2009, 12:47 PM
Hi Ender,

Thank you for sharing your code. I was going to suggest something in the lines of the MVVM pattern, but I expected that you will have client side classes that will represent your entities (ViewModels).

This could save you the effort to create the TreeView by yourself, also you will be able to use things like "ExpandByPath" or "GetItemByPath". This could mean that you will have to save just the path of the currently expanded items.

It seems like this will be a major change for your code, so if you have the app working as it is, you may not need to introduce major changes.

Sincerely yours,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Miguel Rios
Top achievements
Rank 1
answered on 25 Mar 2010, 08:19 PM
Hello Miroslav,

I am interested in the same issue that Ender, i want to perform a search within the TreeView and mark as selected the next item that contains the search text and so on... but, is this possible? the problem with the creation of the elements is still present in the new releases? if it is so... is there an alternative to perform the search that i mentioned?

I am creating the Tree from a flat list that is downloaded from a web service and i also place my business objects in the tag of the TreeViewItem. I've stucked with this for  while, i would be great if you could help me with some ideas or to discart the possibility.

Thank you and greetings.
0
Miroslav
Telerik team
answered on 31 Mar 2010, 11:24 AM
Hi Miguel Rios,

Creating TreeView items is not very scalable solution because TreeView's virtualization does not work in this case.

Working directly with the RadTreeViewItems may be easier at first but the TreeView allows you to bind directly to business objects.

I am attaching an implementation of search-as-you-type  in the TreeView with prev/next support where the TreeView is databound.

The example is based on a converter which takes a flat list of items (like in your case I suppose) and builds the hierarchy of items as needed.

Please note that in the example I generate the hierarchy at random and cycles may appear in the hierarchy and the search algorithm. This of course will not happen in a real scenario.

Greetings,
Miroslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
TreeView
Asked by
Jamest
Top achievements
Rank 2
Answers by
Miroslav
Telerik team
Jamest
Top achievements
Rank 2
Miguel Rios
Top achievements
Rank 1
Share this question
or