New to Telerik UI for WPF? Download free 30-day trial

Expanding and Collapsing Items

The RadTreeListView control allows you to control the state of its items both programmatically via its public methods and through the IsExpandedBinding and IsExpandableBinding properties.

Expand and Collapse Items Programmatically

There are four methods which you can use to programmatically control the state of the items:

  • ExpandHierarchyItem(object hierarchyItem): Expands the respective GridViewRow for the given item.
  • CollapseHierarchyItem(object hierarchyItem): Collapses the respective GridViewRow for the given item.
  • ExpandAllHierarchyItems(): Expands all hierarchy items.
  • CollapseAllHierarchyItems(): Collapses all hierarchy items.

All four methods can also be given an additional boolean parameter (preserveLocalSettings) which determines if the IsExpandable state of the item set via the RowStyle or RowStyleSelector properties should be preserved.

Example 1: Expand the third item after the control is loaded

private void TreeListView_Loaded(object sender, RoutedEventArgs e) 
{ 
    TreeListView.ExpandHierarchyItem(TreeListView.Items[2], true); 
} 

IsExpandedBinding and IsExpandableBinding

The RadTreeListView also exposes the IsExpandedBinding and IsExpandableBinding properties which you can use to synchronize its expanded and expandable states with your viewmodel.

Binding to the IsExpanded property of TreeListViewRow is not fully supported and that is why we recommend using the IsExpandedBinding property instead.

IsExpandedBinding

IsExpandedBinding property can be used to control whether a row is expanded or not.

First start by adding a boolean property to your business items as demonstrated in Example 1. For the purposes of this article, we will extend the WarehouseItem class from the Getting Started article.

Example 2: Extend the WarehouseItem class

public class WarehouseItem : ViewModelBase 
{ 
    // other members 
 
    private bool isExpanded; 
 
    public bool IsExpanded 
    { 
        get 
        { 
            return this.isExpanded; 
        } 
        set 
        { 
            if (value != this.isExpanded) 
            { 
                this.isExpanded = value; 
                this.OnPropertyChanged("IsExpanded"); 
            } 
        } 
    } 
} 
Public Class WarehouseItem       
    Inherits ViewModelBase 
 
    ' other members 
 
    Private m_isExpanded As Boolean 
 
    Public Property IsExpanded() As Boolean 
        Get 
            Return Me.m_isExpanded 
        End Get 
        Set(value As Boolean) 
            If value <> Me.m_isExpanded Then 
                Me.m_isExpanded = value 
                Me.OnPropertyChanged("IsExpanded") 
            End If 
        End Set 
    End Property 
End Class 

Note that the class inherits from the ViewModelBase class which implements the INotifyPropertyChanged interface.

Then define the RadTreeListView control as demonstrated in Example 2.

Example 3: Declare RadTreeListView in XAML

<telerik:RadTreeListView x:Name="radTreeListView" 
                             IsExpandedBinding="{Binding IsExpanded, Mode=TwoWay}" 
                             AutoGenerateColumns="False"> 
        <telerik:RadTreeListView.ChildTableDefinitions> 
            <telerik:TreeListViewTableDefinition ItemsSource="{Binding Items}" /> 
        </telerik:RadTreeListView.ChildTableDefinitions> 
        <telerik:RadTreeListView.Columns> 
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" 
                                Header="Name" /> 
            <telerik:GridViewDataColumn DataMemberBinding="{Binding IsExpanded}"  
                                Header="Is Expanded" /> 
        </telerik:RadTreeListView.Columns> 
    </telerik:RadTreeListView> 

With this setup expanding or collapsing a row will result in the respective checkbox from the IsExpanded column to be updated.

A complete example of using RadTreeListView's IsExpandedBinding property is available in the TreeListView's IsExpanded demo.

IsExpandableBinding

The use of IsExpandableBinding is identical to that of the IsExpandedBinding - you need to define a boolean property in your business class and bind it to the IsExpandableBinding property of the RadTreeListView control.

A complete example of using RadTreeListView's IsExpandableBinding property is available in the TreeListView's OnDemandDataLoading demo.

See Also

In this article