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

Using AutoCompleteBox for RadGridView

11 Answers 163 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Naren
Top achievements
Rank 1
Naren asked on 30 Sep 2010, 08:04 AM
Hi,
I have a RadGridView. Now I want to use Silverlight AutoCompleteBox for this RadGridView.
How can I achieve this...

Any Solution will be helpful.
Thanx.

11 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 30 Sep 2010, 08:09 AM
Hello,

You can declare any control in the grid cells using CellTemplate/CellEditTemplate column properties. 

Greetings,
Vlad
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
0
Naren
Top achievements
Rank 1
answered on 01 Oct 2010, 06:40 AM
Thats correct but can we add RadGridView inside AutoCompleteBox .....

I wanted to add RadGridView inside AutoCompleteBox  so taht i can search TreeViewItems...
Thanx in advance...
0
Vlad
Telerik team
answered on 01 Oct 2010, 06:57 AM
Hello,

 I've never tried this however you may check my blog post which is very similar to your scenario. RadGridView inside RadComboBox with filter as you type. 

Kind regards,
Vlad
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
0
Naren
Top achievements
Rank 1
answered on 01 Oct 2010, 07:50 AM
Sorry it was my mistake

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


Extremely sorry for the mistake.

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


Sorry for inconvenience Vlad.

Thanx in advance
0
Naren
Top achievements
Rank 1
answered on 04 Oct 2010, 03:14 PM
can you help me?
0
Naren
Top achievements
Rank 1
answered on 05 Oct 2010, 05:34 AM
Hi Vlad can you help me ...can you check above image
0
Viktor Tsvetkov
Telerik team
answered on 06 Oct 2010, 09:43 AM
Hello Naren,

I am not sure what you meant with "I want to add RadTreeView inside AutoCompleteBox", but you can examine this example and you can assume that instead RadWatermarkTextBox is used AutoCompleteBox. If you have further questions feel free to ask.

All the best,
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
0
Naren
Top achievements
Rank 1
answered on 07 Oct 2010, 09:37 AM
Hi Viktor Tsvetkov,
             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.
0
Viktor Tsvetkov
Telerik team
answered on 07 Oct 2010, 04:28 PM
Hello Naren,

I am not sure what your problem is. Why do not you just add RadTreeView in the popup and follow the search logic from the example that I have sent to you?

Sincerely yours,
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
0
Andrea
Top achievements
Rank 1
answered on 12 Jun 2012, 08:00 AM
ok, thank.
 then I have on elast question.
I have tried to implement my own Excel formatter incliding Image and data, and more or less i t works.
The annoing part is that when I open the document Excel shows tis red waring bar with "Protected View" message.

i can see that when I sue your DOCXFormatProvider then word does not show the message.
can I know how you avoid that?
0
Dimitrina
Telerik team
answered on 14 Jun 2012, 09:10 AM
Hi,

 As you have stated your question, I am not sure how you have done the export. Would you please clarify what do you export, how have you implemented the export?  

Greetings,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
TreeView
Asked by
Naren
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Naren
Top achievements
Rank 1
Viktor Tsvetkov
Telerik team
Andrea
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or