RadControls for WPF

The RadTreeView API offers BringIntoView support. The RadTreeView and RadTreeViewItem classes expose two methods:

  • BringIndexIntoView()
  • BringItemIntoView()

When you invoke any of these methods, the RadTreeView will attempt to bring the provided item into view.

Note

Both of the methods will only work at the following conditions:

  • They work only for immediate items.
  • They will not work before the item containers have been generated.

Consider the following example. A RadTreeView is data bound to a collection of business objects.

CopyXAML
<Grid x:Name="LayoutRoot"
        Background="White">
    <Grid.Resources>
        <DataTemplate x:Key="treeViewTemplate">
            <TextBlock Text="{Binding Title}" />
        </DataTemplate>
    </Grid.Resources>
    <telerik:RadTreeView x:Name="RadTreeView">
        <telerik:RadTreeViewItem x:Name="RadTreeViewItem"
                                            Header="MyItems"
                                            ItemTemplate="{StaticResource treeViewTemplate}">
        </telerik:RadTreeViewItem>
    </telerik:RadTreeView>
</Grid>
CopyC#
public class MyViewModel 
{
    private static int idCounter;
    private string title;
    public MyViewModel()
    {
        this.Id = idCounter++;
    }
    public int Id
    {
        get;
        protected set;
    }
    public String Title
    {
        get
        {
            return this.title;
        }
        set
        {
            if ( this.title != value )
            {
                this.title = value;
            }
        }
    }
}
//.....
RadTreeViewItem.ItemsSource = Enumerable.Range( 1, 40 ).Select( i => new MyViewModel()
{
    Title = "Item " + i
} ).ToList();
CopyVB.NET
Public Class MyViewModel
 Private Shared idCounter As Integer
 Private m_title As String
 Public Sub New()
  Me.Id = System.Math.Max(System.Threading.Interlocked.Increment(idCounter),idCounter - 1)
 End Sub
 Public Property Id() As Integer
  Get
   Return m_Id
  End Get
  Protected Set
   m_Id = Value
  End Set
 End Property
 Private m_Id As Integer
 Public Property Title() As [String]
  Get
   Return Me.m_title
  End Get
  Set
   If Me.m_title <> value Then
    Me.m_title = value
   End If
  End Set
 End Property
End Class

If you want to programmatically scroll to the "Item 20" (bring this item into view), you could try the following scenario:

CopyC#
private void BringItemIntoView()
{
    this.RadTreeViewItem.BringIndexIntoView( 20 );
}
CopyVB.NET
Private Sub BringItemIntoView()
    Me.RadTreeViewItem.BringIndexIntoView(20)
End Sub

And the result will be:

Note

RadTreeView offers a second method which does exactly the same thing as BringIndexIntoView() method. That's the BringItemIntoView() method.

CopyC#
private void BringItemIntoView()
{
    MyViewModel viewModel = dataSource.First( v => v.Title == "Item 20" );
    this.RadTreeViewItem.BringItemIntoView( viewModel );
}

CopyVB.NET
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
     Dim viewModel As MyViewModel = dataSource.First(Function(v) v.Title = "Item 20")
     Me.RadTreeViewItem.BringItemIntoView(viewModel)
End Sub

The BringItemIntoView() method makes an internal call to the BringIntexIntoView() method.

Tip
Consider using the BringIndexIntoView() method when it's possible.

See Also