How to implement search or filter in RadTreeView

2 posts, 0 answers
  1. Nitin
    Nitin avatar
    19 posts
    Member since:
    Jun 2010

    Posted 17 Oct 2010 Link to this post

    Hi,

    I am using RadTreeView to show a Level Hierarchy. I have a textbox to enter search string and a "Search" button on the screen. When user enters the string and clicks "Search" button, the observable collection should be filtered and the Level that contains the entered string should be shown with their upper hierarchy only upto root.

    I have made a class "Level" which I am using to implement Level hierarchy.

    Here is the code for class

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Collections.ObjectModel;

     

    namespace RadTreeViewSample

    {

        class Level

        {

            public Level(int id, string name, Level parentLevel)

            {

                this.ID = id;

                this.Name = name;

                this.ParentLevel = parentLevel;

            }

     

            public int ID { get; set; }

            public string Name { get; set; }

            public Level ParentLevel { get; set; }

            public ObservableCollection<Level> ChildLevels { get; set; }

     

            public static ObservableCollection<Level> SampleLevelList(int level, Level parentOrg)

            {

                ObservableCollection<Level> orgList = new ObservableCollection<Level>();

                for (int i = 0; i < 5; i++)

                {

                    Level org = new Level(i, "Level " + level.ToString() + "-" + i.ToString(), parentOrg);

                    if (level > 0)

                        org.ChildLevels = SampleLevelList(level - 1, org);

                    orgList.Add(org);

                }

                return orgList;

            }

        }

     

    }

     

     

     

     






    Here is the xaml

    <
    Window x:Class="RadTreeViewSample.MainWindow"

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"

            xmlns:telerikTV="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"

            Title="MainWindow" Height="350" Width="525">

        <Window.Resources>

            <HierarchicalDataTemplate x:Key="items" ItemsSource="{Binding ChildLevels}">

                <TextBlock Text="{Binding Name}"/>

            </HierarchicalDataTemplate>

        </Window.Resources>

        <Grid>

            <Grid.RowDefinitions>

                <RowDefinition Height="auto"/>

                <RowDefinition />

            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>

                <ColumnDefinition />

                <ColumnDefinition Width="auto"/>

            </Grid.ColumnDefinitions>

            <TextBox x:Name="txtOrgname" Grid.Row="0" Grid.Column="0" Margin="5" />

            <Button x:Name="btnSearch" Content="Search" Width="80" Grid.Row="0" Grid.Column="1" Margin="5" Click="btnSearch_Click"/>

            <telerik:RadTreeView x:Name="rtvLevel" ItemTemplate="{StaticResource items}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Margin="5"/>

        </Grid>

    </Window>

     

     

     



    Here is my code-behind

     


    using
    System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Data;

    using System.Windows.Documents;

    using System.Windows.Input;

    using System.Windows.Media;

    using System.Windows.Media.Imaging;

    using System.Windows.Navigation;

    using System.Windows.Shapes;

    using System.Collections.ObjectModel;

     

    namespace RadTreeViewSample

    {

        /// <summary>

        /// Interaction logic for MainWindow.xaml

        /// </summary>

        public partial class MainWindow : Window

        {

            private ObservableCollection<Level> LevelList;

            public MainWindow()

            {

                InitializeComponent();

     

                ObservableCollection<Level> LevelList = Level.SampleLevelList(5, null);

                rtvLevel.ItemsSource = LevelList;

            }

     

            private void btnSearch_Click(object sender, RoutedEventArgs e)

            {

    What should I write here to filter the LevelList, so that only element that contains search string are show

           in treeview with their upper hierarchy only upto root ?

            }

     

        }

    }

     

     

     

     

  2. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 19 Oct 2010 Link to this post

    Hello Nitin,

    To filter RadTreeView in WPF is the same as filtering any other ItemsControl - by using WPF’s CollectionViewSource. Bellow you can find some really useful articles on filtering a collection:

    How do I filter items from a collection?

    How do I apply more than one filter?

    WPF’s CollectionViewSource

    How to filter a wpf treeview hierarchy using an ICollectionView?


    WPF's ICollectionView.filter with large sets of data

    Regards,
    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
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top