Expand and Select Item with Load on Demand Enabled RadTreeView
This tutorial shows how to expand and select an item in RadTreeView with load on demand enabled (IsLoadOnDemandEnabled set to True).
Before you begin, configure a project with Progress® Telerik® UI for WPF. Follow the TreeView getting started guide for references, licensing, the XAML namespace, and theme setup.
The example uses a data-bound RadTreeView and loads child items through the data model rather than the built-in LoadOnDemand event.
Expand and Select an Item
Follow these steps to define the data hierarchy and navigate to an item:
-
Declare the data-bound
RadTreeView:XAML<Grid> <Grid.Resources> <HierarchicalDataTemplate x:Key="treeViewTemplate" ItemsSource="{Binding Children}"> <TextBlock Text="{Binding Title}" /> </HierarchicalDataTemplate> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="300" /> </Grid.ColumnDefinitions> <telerik:RadTreeView x:Name="treeView2" Margin="8" HorizontalAlignment="Stretch" IsLoadOnDemandEnabled="True" IsVirtualizing="True" ItemTemplate="{StaticResource treeViewTemplate}" telerik:TextSearch.TextPath="Title" /> <StackPanel Grid.Column="1" Width="300" HorizontalAlignment="Right" VerticalAlignment="Top"> <Button Click="Button_Click_1" Content="Bring Item" /> <TextBox x:Name="userInput2" /> </StackPanel> </Grid> -
Define the
DataItemclass for the items in the data source:C#public class DataItem : ViewModelBase { private string title; private List<DataItem> children; public String Title { get { return this.title; } set { if (this.title != value) { this.title = value; this.OnPropertyChanged("Title"); } } } public List<DataItem> Children { get { return this.children; } } } -
Create the initial data and set the
ItemsSourceproperty of theRadTreeView:C#this.treeView.ItemsSource = Enumerable.Range(1, 10).Select(i => new DataItem() { Title = string.Format("Item {0}", i) }).ToList(); -
Implement load on demand in the getter of the
DataItem.Childrenproperty:The load on demand feature displays an expander icon in front of each
RadTreeViewItem. This example does not use the built-inLoadOnDemandevent to load items. Instead, theHierarchicalDataTemplatebinding evaluates theChildrengetter on load. The getter loads a fixed number of items when thechildrenfield is not yet initialized.C#public List<DataItem> Children2 { get { if (this.children == null) { this.children = new List<DataItem>(); for (int i = 0; i < 5; i++) { DataItem item = new DataItem() { Title = string.Format("{0}.{1}", this.Title, i) }; this.children.Add(item); } } return this.children; } } -
Implement the custom path-building logic for the
BringPathIntoView()method:For path matching, set Telerik's
TextSearch.TextPathattached property to the data property used for each path segment.BringPathIntoView()traverses the data hierarchy and can bring an item into view before all item containers exist. Configure selection separately. For a selection-binding example, see bringing an item into view and selecting it.C#private void Button_Click_1(object sender, RoutedEventArgs e) { string[] headerAndIndexes = this.userInput.Text.Split(' '); string[] indexes = headerAndIndexes.LastOrDefault().Split('.'); bool areIndexesValid = false; int i; if (headerAndIndexes != null && indexes != null) { foreach (string index in indexes) { areIndexesValid = int.TryParse(index, out i); } if (headerAndIndexes.FirstOrDefault().Contains("Item") && areIndexesValid) { RadTreeViewItem item = this.treeView.GetItemByPath(this.GenetatePath(this.userInput.Text)); if (item != null) { item.IsSelected = true; item.IsExpanded = true; } } } } private string GenetatePath(string itemToExpand) { string path = string.Empty; string buffer; string[] header = itemToExpand.Split(' '); string[] indexes = header.LastOrDefault().Split('.'); header[0] = string.Format("{0} {1}", header[0], indexes[0]); path = string.Format("{0}\\", header[0]); for (int i = 1; i < indexes.Length; i++) { buffer = string.Format("{0}.{1}", header[0], indexes[i]); header[0] = buffer; path += string.Format("{0}\\", buffer); } return path.TrimEnd('\\'); }
The following image shows the tree before and after navigation to Item 2.3.1.4.
