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 |
} |
9 Answers, 1 is accepted
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.
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 == null) return; selectedContainer.BringIntoView(); }, DispatcherPriority.Background);
Any tips on how to handle it correctly?
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.
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?
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?
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/.
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.
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/.