This is a migrated thread and some comments may be shown as answers.

Set selected item and get selected container

9 Answers 560 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Lauren Nickerson
Top achievements
Rank 1
Lauren Nickerson asked on 30 Dec 2009, 07:54 PM
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" }; 
 
            container.AddFolder(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.

Thanks!

9 Answers, 1 is accepted

Sort by
0
Valentin.Stoychev
Telerik team
answered on 04 Jan 2010, 08:48 AM
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,
Valentin.Stoychev
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.
0
Pieter Jan Verfaillie
Top achievements
Rank 1
answered on 21 Nov 2012, 09:29 AM
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;
 
                    selectedContainer.BringIntoView();
                }, DispatcherPriority.Background);

Any tips on how to handle it correctly?
0
Pavel R. Pavlov
Telerik team
answered on 26 Nov 2012, 08:37 AM
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.

0
Madhan
Top achievements
Rank 1
answered on 29 Jan 2017, 02:33 PM
Thank you this fixed my issue.
0
Paul
Top achievements
Rank 1
answered on 09 Apr 2021, 11:51 AM

I found this topic and the code in the attached zip (by Pavel) and was happy to see it does what I need. However I need a little bit more: what I need is that edit mode is started and the cursor is put in the first field textbox (my actual code uses templates with multiple fields) so that the user can just start typing.

In code I tried adding after xTreeViewItem.IsSelected = true;:

xTreeViewItem.IsInEditMode = true;

or

xTree.SelectedContainer.IsInEditMode = true;

 

but these solutions both result in unusal behavior: the item seems unaffected until the mouse hovers above the item, it then changes into an empty edit field and I still need to put the cursor in that empty field.

Any idea's on how to make this work? Why doesn't this work like you would expect? 

 

0
Paul
Top achievements
Rank 1
answered on 09 Apr 2021, 02:08 PM

I just found out that when I take the original code and just replace

IsSelected = true

with

IsInEditMode = true

It almost works, but only after I do an initial first click on the new item. After that it works as intended. Huh? Why not the first time?

0
Dinko | Tech Support Engineer
Telerik team
answered on 14 Apr 2021, 06:54 AM

Hello Paul,

May I ask you to share the custom template of the RadTreeView? This way I can try to mimic your set-up on my side to better understand it and think of a possible solution.

Regards,
Dinko
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Paul
Top achievements
Rank 1
answered on 19 Apr 2021, 09:55 AM

I already managed to find a solution that works, I just replaced

xTreeViewItem.IsSelected = true

with this code

Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => {  xTreeViewItem.IsInEditMode = true; }), DispatcherPriority.Loaded);

This will make sure that the new item is loaded before edit mode is started.

This behaves as you would expect, both in the example code as in my application.

0
Dinko | Tech Support Engineer
Telerik team
answered on 20 Apr 2021, 06:53 AM

Hi Paul,

I am happy to hear that you have found a solution for your scenario and thank you for sharing it with the community. If you have any other questions, you can open a new forum thread and we will be happy to help.

Regards,
Dinko
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
TreeView
Asked by
Lauren Nickerson
Top achievements
Rank 1
Answers by
Valentin.Stoychev
Telerik team
Pieter Jan Verfaillie
Top achievements
Rank 1
Pavel R. Pavlov
Telerik team
Madhan
Top achievements
Rank 1
Paul
Top achievements
Rank 1
Dinko | Tech Support Engineer
Telerik team
Share this question
or