Select topnode by default

2 posts, 0 answers
  1. Sunandini
    Sunandini avatar
    4 posts
    Member since:
    Apr 2009

    Posted 21 Apr 2009 Link to this post

    I have a WPF desktop app. I have a tree view. I load the tree view using an observable collection. I want to select the topmost node of the tree by default, to start with. But SelectedItem, SelectedValue etc seem to be readonly. Is it possible to select the top node of the tree when I start up?

    Thanks
  2. Bobi
    Admin
    Bobi avatar
    513 posts

    Posted 22 Apr 2009 Link to this post

    Hello Sunandini,

    You can try the following:

    1.Add some ItemsSource to RadtreeView:
          this.treeView.ItemsSource = listCollection;

    2.Subscribe to RadtreeView Loaded :
         this.treeView.Loaded += new RoutedEventHandler(TreeView_Loaded);

    3.Add the following code:
    private void TreeView_Loaded(object sender, RoutedEventArgs e)
            {
                RadTreeViewItem item = treeView.ItemContainerGenerator.ContainerFromIndex(1) as RadTreeViewItem;
                if(item != null)
                {
                item.IsSelected = true;
                }
            }

    In order to select a RadtreeView node all you have to do is simply to get an instance of the desired RadtreeViewItem and set its IsSelected property to true.

    Note:If you use binding you can get the desired RadTreeViewItem  by using  the following:
          
    ItemContainerGenerator.ContainerFromIndex(index) or ItemContainerGenerator.ContainerFromItem(item).

    I hope this answers your question.


    Best wishes,
    Boryana
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top