Set selected item and get selected container

5 posts, 0 answers
  1. Lauren Nickerson
    Lauren Nickerson avatar
    52 posts
    Member since:
    Jul 2009

    Posted 30 Dec 2009 Link to this post

    Hi. I'm generating a RadTreeView and its items through WPF data binding using a HierarchicalDataTemplate. So when I add a new item to the collection the RadTreeView is looking at, and I set the selected item to that newly added object, I try to get the selected container, but it's null. This is my code:

    private void OnAddFolderClicked(object sender, RoutedEventArgs e) 
                IFolderContainer container = (sender as MenuItem).DataContext as IFolderContainer ?? null

                Folder folder = new Folder() { Name = "Folder" }; 
                treeViewExplorer.SelectedItem = folder; 
                folder.IsNodeInEditMode = true
                RadTreeViewItem selectedItem = treeViewExplorer.SelectedContainer; // this is null even though I set the SelectedItem = folder

    What I want to do is to set focus on it, so if the user clicks F2 right away after creating it, it can go into edit mode, right now the object looks selected, but hitting F2 doesn't do anything unless deselect it, and select it again.

  2. Valentin.Stoychev
    Valentin.Stoychev avatar
    2198 posts

    Posted 04 Jan 2010 Link to this post

    Hi Lauren Nickerson,

    The problem is that when you want to get the SelectedContainer the container is still not generated, because of the async nature of process when you add new item.

    You need to put the code for getting the selected container in a Dispatcher call, so that the container is already generated when you try to get it.

    Best wishes,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Pieter Jan Verfaillie
    Pieter Jan Verfaillie avatar
    11 posts
    Member since:
    Dec 2009

    Posted 21 Nov 2012 Link to this post

    Hi Lauren, 

    Did you get this to work? I have very same problem. I've tried to use following code, but it didn't work.

    Dispatcher.InvokeAsync(() =>
                        var selectedContainer = treeview.SelectedContainer;
                        if (selectedContainer == nullreturn;
                    }, DispatcherPriority.Background);

    Any tips on how to handle it correctly?
  4. Pavel R. Pavlov
    Pavel R. Pavlov avatar
    1237 posts

    Posted 26 Nov 2012 Link to this post

    Hello Pieter,

    Let me go straight to your issue. The BringPathIntoView method will force the RadTreeView to expand a certain RadTreeViewItem. By doing so, the RadTreeView control will generate the containers for all expanded items. You can read more about this method here. After expanding the newly created item you will be able to get its container. Please take a look ate the attached project where I have implemented the described approach.

    All the best,
    Pavel R. Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  5. Madhan
    Madhan avatar
    2 posts
    Member since:
    Aug 2013

    Posted 29 Jan in reply to Valentin.Stoychev Link to this post

    Thank you this fixed my issue.
Back to Top