Click here to Skip to main content
15,909,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am doing a Search in my Treeview and highlighting the searchedText(which works perfect). My next step is with Up and Down arrow key, I need to move only to the Highlighted search item one by one, not to the parents even if it is Visible. With Down Key, it moves properly to the Highlighted item one by one. (Works Perfect)
But with Up key, it is not moving at all.Loop is recursing and always returns null. I tried various ways, but finding it difficult to figure it out. Please Help. I am stuck with this for about a week.

What I have tried:

C#
<pre> private void DrinksTreeView_PreviewKeyDown(object sender, KeyEventArgs e)
        {

            if (e.Key == Key.Tab || e.Key == Key.Down)
            {
               TreeViewItem selectedItem = (SearchTreeView.SelectedItem) as TreeViewItem;
                    if (selectedItem != null)
                    {
                        TreeViewItem nextItem = FindNextFocusableItem(selectedItem, true);
                        if (nextItem != null)
                        {
                            nextItem.IsSelected = true;
                        }
                    }

                    e.Handled = true;

            }
            else
            if (e.Key == Key.Up)
            {
               TreeViewItem selectedItem = (SearchTreeView.SelectedItem) as TreeViewItem;
                if (selectedItem != null)
                {
                    Keyboard.Focus(SearchTreeView.SelectedItem as IInputElement);
                    FindPreviousItem(selectedItem);
                   
                }
                e.Handled = true;
            }
        }

This function performs the Down Arrow Key Event
C#
<pre> private TreeViewItem FindNextFocusableItem(TreeViewItem currentItem, bool gosubTreeView)
         {
           
            if (gosubTreeView && currentItem.IsExpanded)
            {
                  TreeViewItem item = currentItem.ItemContainerGenerator.ContainerFromIndex(0) as TreeViewItem;
                 
                if (item != null)
                {
                   
                    string itemHeader = item.Header.ToString().Trim();
                    findAll = itemHeader.IndexOf(txtSearch.Text, StringComparison.OrdinalIgnoreCase) >= 0;
                    
                    if (item.Visibility == Visibility.Visible)
                    {
                        
                        if (findAll == true)
                        {
                            return item;
                        }
                        else
                        {
                           return FindNextFocusableItem(item, true);
                        }
                       
                       
                    }
                    return FindNextFocusableItem(item, false);

                }
            }
            ItemsControl parentItemsControl = ItemsControl.ItemsControlFromItemContainer(currentItem);
            if (parentItemsControl != null)
            {
                TreeViewItem item2;
                int index = parentItemsControl.ItemContainerGenerator.IndexFromContainer(currentItem);
                int count = parentItemsControl.Items.Count;
                while (index < count)
                {
                    index++;
                    item2 = parentItemsControl.ItemContainerGenerator.ContainerFromIndex(index) as TreeViewItem;
                    
                    if ((item2 != null)) 
                    {
                        
                        string itemHeader = item2.Header.ToString().Trim();
                        findMe = itemHeader.IndexOf(txtSearch.Text, StringComparison.OrdinalIgnoreCase) >= 0;
                        if (item2.Visibility == Visibility.Visible  )
                        {
                            if (findMe == true)
                            {
                                return item2;
                            }
                            else
                            {
                                return FindNextFocusableItem(item2, true);
                            }

                        }
                       
                    }
                }
                item2 = parentItemsControl as TreeViewItem;
                if ((item2 != null))
                {
                    return FindNextFocusableItem(item2, false);
                }
                return null;
            }
            return null;
        }

This function performs the Up arrow Key Event
C#
<pre> private TreeViewItem FindPreviousItem(TreeViewItem thisItem)
        {
            if (!(((TreeViewItem)thisItem).Parent is TreeView))
            {
                ItemCollection nodes = ((TreeViewItem)((TreeViewItem)thisItem).Parent).Items;
                for (int i = 0; i < nodes.Count; i++)
                {
                    TreeViewItem whatItem = (TreeViewItem)nodes[i];
                    if (nodes[i].Equals(thisItem))
                    {
                        try
                        {
                           
                            if (i > 0)
                            {
                               
                                TreeViewItem whatnextItem = (TreeViewItem)nodes[i-1];
                             
                                if (whatnextItem.IsVisible == true)
                                {
                                   
                                    string itemHeader = whatnextItem.Header.ToString().Trim();
                                    findMe = itemHeader.IndexOf(txtSearch.Text, StringComparison.OrdinalIgnoreCase) >= 0;
                                     if (findMe == true)
                                    {
                                        whatnextItem.IsSelected = true;
                                       
                                    }
                                    else
                                    {
                                       FindPreviousItem(whatnextItem);
                                    }
                                  
                                }
                                
                            }
                            else
                            {
                                TreeViewItem whichItem = (TreeViewItem)((TreeViewItem)nodes[i]).Parent;
                                if (whichItem.IsVisible == true)
                                {
                                    string itemHeader = whichItem.Header.ToString().Trim();
                                    findAll = itemHeader.IndexOf(txtSearch.Text, StringComparison.OrdinalIgnoreCase) >= 0;
                                 
                                    if (findAll == true)
                                    {
                                        whichItem.IsSelected = true;
                                    }
                                    else
                                    {
                                        FindPreviousItem(whichItem);
                                    }
                                   
                                }
                                else
                                {
                                   FindPreviousItem(whichItem);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }

                     
                    }
                }
                return null;
            }
            return null;
        }
Posted
Updated 12-Sep-19 6:35am

1 solution

Too busy.

Create a "shadow" collection of selected items and index through that. Use the selected item from the shadow to sync the "main" view.
 
Share this answer
 
Comments
Priya Karthish 13-Sep-19 6:46am    
Thanks Gerry. Your idea worked out. I found the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900