Using AutoCompleteBox for RadTreeView

6 posts, 0 answers
  1. Naren
    Naren avatar
    53 posts
    Member since:
    Aug 2010

    Posted 05 Oct 2010 Link to this post

    I wanted to add RadTreeView inside AutoCompleteBox  so taht i can search TreeViewItems.




    I want to do the something like in attached image...
    Please look into attached Image.

    Can you help me out..
    Thanx in advance
  2. Nick
    Nick avatar
    17 posts
    Member since:
    Oct 2010

    Posted 06 Oct 2010 Link to this post

    I would also be interested in that
  3. DevCraft banner
  4. Naren
    Naren avatar
    53 posts
    Member since:
    Aug 2010

    Posted 07 Oct 2010 Link to this post

    Hi Telerik team,
                 Can you check my following code to understand what i exactly wants(Can you Create a silverlight application and paste this code in it and then run it so that you will understand the question correctly).

    My Xaml is like foolows
    <UserControl x:Class="AutoCompleteTreeSearch.MainPage"
                 xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls"
                 xmlns:local="clr-namespace:AutoCompleteTreeSearch"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
        <UserControl.Resources>
            <common:HierarchicalDataTemplate x:Key="AEmail" ItemsSource="{Binding SubClass}">
                <StackPanel>
                    <TextBlock Margin="0,0,0,0" Text="{Binding Path=Header}"/>
                </StackPanel>
            </common:HierarchicalDataTemplate>
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White">
            <Popup x:Name="PopupControl" MaxHeight="10" ScrollViewer.VerticalScrollBarVisibility="Auto">
                <local:SilverlightTreeView x:Name="tvData" MaxHeight="200" ScrollViewer.VerticalScrollBarVisibility="Auto" ItemTemplate="{StaticResource AEmail}" SelectedItemChanged="tvData_SelectedItemChanged"  KeyDown="tvData_KeyDown"  GotFocus="tvData_GotFocus"  MouseLeftButtonUp="tvData_MouseLeftButtonUp"  LostFocus="tvData_LostFocus" >
                </local:SilverlightTreeView>
            </Popup>
    <TextBox MaxHeight="30" MinWidth="100" TextChanged="txtSearchBox_TextChanged"  x:Name="txtSearchBox" KeyUp="txtSearchBox_KeyUp"  KeyDown="txtSearchBox_KeyDown"  GotFocus="txtSearchBox_GotFocus"  LostFocus="txtSearchBox_LostFocus"  Margin="5,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Stretch"></TextBox>
        </Grid>
    </UserControl>
    And my Mainpage.xaml.cs is as follows
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Windows.Data;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
     
    namespace AutoCompleteTreeSearch
    {
        public partial class MainPage : UserControl
        {
            #region Variable Declarations
            TreeViewItem tv;
            HierarchicalTreeData selectedTV;
            HierarchicalTreeData SelectedClass;
            bool selectedText;
            bool IsTextboxFocus = false;
            long LastTicks;
            #endregion
            public MainPage()
            {
                InitializeComponent();
                tvData.ItemsSource = Model.GetData();   //Bind Actual TreeView Data
            }
     
     
            #region TreeView Events
            private void tvData_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
            {
                if (tvData.SelectedItem != null)
                {
                    if (tvData.SelectedItem.GetType().ToString() == "AutoCompleteTextBox.HierarchicalCity")
                    {
                        tv = tvData.ContainerFromItem(tvData.SelectedItem);
                        if (IsTextboxFocus == false)
                        {
                            txtSearchBox.Text = ((HierarchicalTreeData)tvData.SelectedItem).Header;
                            selectedText = true;
                        }
                        SelectedClass = (HierarchicalTreeData)tvData.SelectedItem;
                    }
                    else if (tvData.SelectedItem.GetType().ToString() == "AutoCompleteTextBox.SilverlightTreeViewItem")
                    {
                        tv = (TreeViewItem)tvData.SelectedItem;
                    }
                }
            }
     
            private void tvData_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.Escape || e.Key == Key.Enter)
                {
                    if (e.Key == Key.Enter && SelectedClass != null)
                    {
                        txtSearchBox.Text = SelectedClass.Header;
                        selectedText = true;
                    }
                    txtSearchBox.Focus();
                    HideTreeview();
                }
            }
     
            private void tvData_GotFocus(object sender, RoutedEventArgs e)
            {
                GoToItem();
            }
     
            private void tvData_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                if ((DateTime.Now.Ticks - LastTicks) < 2310000)
                {
                    if (SelectedClass != null)
                    {
                        selectedText = true;
                        txtSearchBox.Text = SelectedClass.Header;
                    }
                    txtSearchBox.Focus();
                    HideTreeview();
                    //PopupControl.IsOpen = false;
                }
                LastTicks = DateTime.Now.Ticks;
            }
     
            private void tvData_LostFocus(object sender, RoutedEventArgs e)
            {
                CheckFocus();
            }
            #endregion
     
            #region Textbox Events
            private void txtSearchBox_TextChanged(object sender, TextChangedEventArgs e)
            {
                if (txtSearchBox.Text == "")
                {
                    HideTreeview();
                }
                else if (selectedText == false)
                {
                    GoToItem();
                }
            }
     
            private void txtSearchBox_KeyUp(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.Enter && SelectedClass != null)
                {
                    txtSearchBox.Text = SelectedClass.Header;
                    selectedText = true;
                    HideTreeview();
                }
                else if ((e.Key == Key.Down || e.Key == Key.Up) && tv != null)
                {
                    tv.Focus();
                }
                else if (e.Key == Key.Escape)
                {
                    HideTreeview();
                }
            }
     
            private void txtSearchBox_KeyDown(object sender, KeyEventArgs e)
            {
                selectedText = false;
            }
     
            private void txtSearchBox_GotFocus(object sender, RoutedEventArgs e)
            {
                IsTextboxFocus = true;
            }
     
            private void txtSearchBox_LostFocus(object sender, RoutedEventArgs e)
            {
                CheckFocus();
                IsTextboxFocus = false;
            }
            #endregion
     
            #region Other Functions
            void CheckFocus()
            {
     
                DependencyObject dp = ((DependencyObject)System.Windows.Input.FocusManager.GetFocusedElement());
                if (dp != null)
                {
                    if (((Control)dp).Name == "txtCity" || dp.GetType().Name == "SilverlightTreeViewItem" || dp.GetType().Name == "SilverlightTreeView")
                    {
                    }
                    else
                    {
                        HideTreeview();
                    }
                }
                else
                {
                    HideTreeview();
                }
            }
     
            private void HideTreeview()
            {
                PopupControl.IsOpen = false;
                tv = null;
                SelectedClass = null;
                foreach (HierarchicalTreeData item in tvData.Items)
                {
                    item.CollapseAll();
                }
            }
     
            private void ReturnConditions(string val)
            {
                selectedTV = null;
                for (int i = 0; i < Model.GetCurrentData().Count; i++)
                {
     
     
                    if (Model.GetCurrentData()[i].Header.StartsWith(val, StringComparison.OrdinalIgnoreCase))
                    {
                        selectedTV = Model.GetCurrentData()[i];
                        ExpandAllParents(Model.GetCurrentData()[i]);
                    }
                    else if (Model.GetCurrentData()[i].SubClass != null)
                    {
                        ReturnChildconditionsRecursively(Model.GetCurrentData()[i].SubClass, val);
                    }
                }
                if (selectedTV == null)
                {
                    HideTreeview();
                }
            }
     
            private void ReturnChildconditionsRecursively(ObservableCollection<HierarchicalTreeData> lst, string val)
            {
                for (int j = 0; j < lst.Count; j++)
                {
                    if (lst[j].Header.StartsWith(val, StringComparison.OrdinalIgnoreCase))
                    {
                        selectedTV = lst[j];
                        ExpandAllParents(lst[j]);
                        break;
                    }
                    else if (lst[j].SubClass != null)
                    {
                        ReturnChildconditionsRecursively(lst[j].SubClass, val);
                    }
                }
     
            }
     
            private void ExpandAllParents(HierarchicalTreeData t)
            {
                SelectedClass = t;
                foreach (HierarchicalTreeData item in tvData.Items)
                {
                    if (item.ExpandSuperclasses(t))
                    {
                        t.IsSelected = true;
                        break;
                    }
                }
     
            }
     
            void GoToItem()
            {
                PopupControl.HorizontalOffset = 31;
                PopupControl.VerticalOffset = 49;
                PopupControl.IsOpen = true;
                ReturnConditions(txtSearchBox.Text);
            }
            #endregion
        }
     
        public class SilverlightTreeView : TreeView
        {
            protected override DependencyObject GetContainerForItemOverride()
            {
                SilverlightTreeViewItem tvi = new SilverlightTreeViewItem();
                Binding expandedBinding = new Binding("IsExpanded");
                expandedBinding.Mode = BindingMode.TwoWay;
                tvi.SetBinding(SilverlightTreeViewItem.IsExpandedProperty, expandedBinding);
                Binding selectedBinding = new Binding("IsSelected");
                selectedBinding.Mode = BindingMode.TwoWay;
                tvi.SetBinding(SilverlightTreeViewItem.IsSelectedProperty, selectedBinding);
                return tvi;
     
            }
        }
        public class SilverlightTreeViewItem : TreeViewItem
        {
            protected override DependencyObject GetContainerForItemOverride()
            {
                SilverlightTreeViewItem tvi = new SilverlightTreeViewItem();
                Binding expandedBinding = new Binding("IsExpanded");
                expandedBinding.Mode = BindingMode.TwoWay;
                tvi.SetBinding(SilverlightTreeViewItem.IsExpandedProperty, expandedBinding);
                Binding selectedBinding = new Binding("IsSelected");
                selectedBinding.Mode = BindingMode.TwoWay;
                tvi.SetBinding(SilverlightTreeViewItem.IsSelectedProperty, selectedBinding);
                return tvi;
            }
        }
     
        public class HierarchicalTreeData : INotifyPropertyChanged
        {
            #region Constructors
            public HierarchicalTreeData(string header)
            {
                Header = header;
                SubClass = new ObservableCollection<HierarchicalTreeData>();
            }
            public HierarchicalTreeData(string header, ObservableCollection<HierarchicalTreeData> sub)
            {
                Header = header;
                SubClass = sub;
            }
            #endregion
     
            #region Data Properties.
            private string _Header = "";
            public string Header
            {
                get { return _Header; }
                set { _Header = value; OnPropertyChanged("Header"); }
            }
     
            private ObservableCollection<HierarchicalTreeData> _SubClass = new ObservableCollection<HierarchicalTreeData>();
            public ObservableCollection<HierarchicalTreeData> SubClass
            {
                get { return _SubClass; }
                set { _SubClass = value; OnPropertyChanged("SubClass"); }
            }
            private bool isSelected;
            public bool IsSelected
            {
                get { return isSelected; }
                set
                {
                    isSelected = value;
                    OnPropertyChanged("IsSelected");
                }
            }
            private bool isExpanded;
            public bool IsExpanded
            {
                get { return isExpanded; }
                set
                {
                    isExpanded = value;
                    OnPropertyChanged("IsExpanded");
                }
            }
            #endregion
     
            #region INotify Implementation
            public event PropertyChangedEventHandler PropertyChanged;
     
            protected virtual void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            #endregion
     
            #region Expansion / Collapse Function
            public bool ExpandSuperclasses(HierarchicalTreeData itemToLookFor)
            {
                return ApplyActionToSuperclasses(itemToLookFor, superclass => superclass.IsExpanded = true);
            }
     
            public void CollapseAll()
            {
                ApplyActionToAllItems();//item => item.IsExpanded = false);
                //ApplyActionToAllItems(item => item.IsSelected = false);
            }
     
            void ActionCollapse(HierarchicalTreeData item)
            {
                item.IsExpanded = false;
                item.IsSelected = false;
            }
     
            private void ApplyActionToAllItems()//Action<HierarchicalCity> itemAction)
            {
                Stack<HierarchicalTreeData> dataItemStack = new Stack<HierarchicalTreeData>();
                dataItemStack.Push(this);
     
                while (dataItemStack.Count != 0)
                {
                    HierarchicalTreeData currentItem = dataItemStack.Pop();
                    ActionCollapse(currentItem);//itemAction(currentItem);
                    foreach (HierarchicalTreeData childItem in currentItem.SubClass)
                    {
                        dataItemStack.Push(childItem);
                    }
                }
            }
     
            private bool ApplyActionToSuperclasses(HierarchicalTreeData itemToLookFor, Action<HierarchicalTreeData> itemAction)
            {
                if (itemToLookFor == this)
                {
                    return true;
                }
                else
                {
                    foreach (HierarchicalTreeData subclass in this.SubClass)
                    {
                        bool foundItem = subclass.ApplyActionToSuperclasses(itemToLookFor, itemAction);
                        if (foundItem)
                        {
                            itemAction(this);
                            return true;
                        }
                    }
                    return false;
                }
            }
     
            #endregion
     
        }
     
        /// <summary>
        /// Set Tree View Data here in "Model class"
        /// </summary>
        public class Model
        {
            static ObservableCollection<HierarchicalTreeData> lst;
     
            public static ObservableCollection<HierarchicalTreeData> GetCurrentData()
            {
                return lst;
            }
            public static ObservableCollection<HierarchicalTreeData> GetData()
            {
                lst = new ObservableCollection<HierarchicalTreeData>();
     
                HierarchicalTreeData levelOne = new HierarchicalTreeData("Understand the Market");
                HierarchicalTreeData levelOneOne = new HierarchicalTreeData("Understand the External Environment");
                HierarchicalTreeData levelOneThree = new HierarchicalTreeData("Develop Vision and Strategy");
                HierarchicalTreeData levelOneThreeOne = new HierarchicalTreeData("Assess and Align Overall Mission and Vision");
                HierarchicalTreeData levelOneThreeTwo = new HierarchicalTreeData("Assess and Align the Business Concept");
                levelOneThree.SubClass.Add(levelOneThreeOne);
                levelOneThree.SubClass.Add(levelOneThreeTwo);
                HierarchicalTreeData levelOneFive = new HierarchicalTreeData("Define Financial Strategy");
                levelOne.SubClass.Add(levelOneOne);
                levelOne.SubClass.Add(levelOneThree);
                levelOne.SubClass.Add(levelOneFive);
                lst.Add(levelOne);
                return lst;
            }
        }
     
        public static class TreeViewExtensions
        {
            public static TreeViewItem ContainerFromItem(this TreeView treeView, object item)
            {
                TreeViewItem containerThatMightContainItem = (TreeViewItem)treeView.ItemContainerGenerator.ContainerFromItem(item);
                if (containerThatMightContainItem != null)
                    return containerThatMightContainItem;
                else
                    return ContainerFromItem(treeView.ItemContainerGenerator, treeView.Items, item);
            }
     
            private static TreeViewItem ContainerFromItem(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection, object item)
            {
                foreach (object curChildItem in itemCollection)
                {
                    TreeViewItem parentContainer = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);
                    if (parentContainer == null)
                        return null;
                    TreeViewItem containerThatMightContainItem = (TreeViewItem)parentContainer.ItemContainerGenerator.ContainerFromItem(item);
                    if (containerThatMightContainItem != null)
                        return containerThatMightContainItem;
                    TreeViewItem recursionResult = ContainerFromItem(parentContainer.ItemContainerGenerator, parentContainer.Items, item);
                    if (recursionResult != null)
                        return recursionResult;
                }
                return null;
            }
     
            public static object ItemFromContainer(this TreeView treeView, TreeViewItem container)
            {
                TreeViewItem itemThatMightBelongToContainer = (TreeViewItem)treeView.ItemContainerGenerator.ItemFromContainer(container);
                if (itemThatMightBelongToContainer != null)
                    return itemThatMightBelongToContainer;
                else
                    return ItemFromContainer(treeView.ItemContainerGenerator, treeView.Items, container);
            }
     
            private static object ItemFromContainer(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection, TreeViewItem container)
            {
                foreach (object curChildItem in itemCollection)
                {
                    TreeViewItem parentContainer = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);
                    if (parentContainer == null)
                        return null;
                    TreeViewItem itemThatMightBelongToContainer = (TreeViewItem)parentContainer.ItemContainerGenerator.ItemFromContainer(container);
                    if (itemThatMightBelongToContainer != null)
                        return itemThatMightBelongToContainer;
                    TreeViewItem recursionResult = ItemFromContainer(parentContainer.ItemContainerGenerator, parentContainer.Items, container) as TreeViewItem;
                    if (recursionResult != null)
                        return recursionResult;
                }
                return null;
            }
        }
    }

    Can you create a Silverlight application and paste above code and check what I exactly wanted..

    Here instead of SimpleTreeView I wanted RadTreeView...

    Thank you very much.
  5. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 07 Oct 2010 Link to this post

    Hi Nick,

    Could you please follow this forum thread.

    Sincerely yours,
    Kiril Stanoev
    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
  6. Naren
    Naren avatar
    53 posts
    Member since:
    Aug 2010

    Posted 07 Oct 2010 Link to this post

    Hi Kiril ,
    That thread was actually created with misspelled Title(There must be RadTreeView instead of RadGridView in Title)..
    So I started this thread.

    Thank you.
    It will be great if you answer this.
    Thanks.
  7. Viktor Tsvetkov
    Admin
    Viktor Tsvetkov avatar
    382 posts

    Posted 07 Oct 2010 Link to this post

    Hi Narendra Kedari,

    You can keep following this forum thread.

    Best wishes,
    Viktor Tsvetkov
    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
Back to Top
DevCraft banner