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 ?
}
}
}