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

Set selected item and get selected container

10 Answers 253 Views
TreeListView
This is a migrated thread and some comments may be shown as answers.
VH
Top achievements
Rank 1
VH asked on 10 Mar 2010, 11:36 PM
Hi, I saw this thread in WPF forum, I have the same problem with RadTreeListView in Silverlight.

"Hi. I'm generating a RadTreeView and its items through Silverlight 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."

Here is your suggestions:

"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."

I have tried to put my BeginEdit in a Dispatcher call, but the container is still null. SelectedItem is bound to an object in my ViewModel which is not null.   

private void NewValueAdded(object sender, EventArgs e)  
{  
   Dispatcher.BeginInvoke(()=>  
   {  
      TreeList.SelectedContainer.BeginEdit();  
   });  

 


Thanks,
VH

10 Answers, 1 is accepted

Sort by
0
Miro Miroslavov
Telerik team
answered on 15 Mar 2010, 04:49 PM
Hi VH,

You can get the container after it's sure that the ItemContainerGenerator created it.

treeList.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);


void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
        {
            var container = treeList.ItemContainerGenerator.ContainerFromItem(treeList.SelectedItem) as RadTreeListViewItem;
            if (container != null)
            {
                container.IsSelected = true;
                container.Focus();
            }
        }

StatusChanged is raised when it generated the newly created TreeListViewItem.
Just to notice that RadTreeListView is in CTP, so there are issues and you shouldn't use it for production code.
Hope this helps you.

Sincerely yours,
Miro Miroslavov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
VH
Top achievements
Rank 1
answered on 15 Mar 2010, 11:32 PM
Hi,

Thank you very much, that works fine for me. :)
 
void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)  
{  
     var container = treeList.ItemContainerGenerator.ContainerFromItem(treeList.SelectedItem) as RadTreeListViewItem;  
     if (container != null)  
     {  
          container.IsSelected = true;  
          container.Focus();  
          container.IsInEditMode = true;
          container.BeginEdit();
     }  

Further question is, I would like to put the new RadTreeListViewItem into edit mode, so I can directly use keyboard to modify it. In the code above, it made the container into edit mode, but the mouse cursor is not there, and I still have to click on it to edit. Did I miss anything?

Cheers,

VH
0
Miro Miroslavov
Telerik team
answered on 16 Mar 2010, 02:49 PM
Hello VH,

After little investigation found that you should remove the container.Focus() line of code, because the editor is actually in a popup that is not the container itself and this way we force the focus not to go to the editor but the item that is behind it. Can you try if removing this line will work for you (The popup when is shown gets the focus by default). One more thing that can help you is the EditorPrepare event of the TreeListViewItem class, where you can get the actual editor item and focus it.
If you have further questions, please contact us.

Regards,
Miro Miroslavov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
VH
Top achievements
Rank 1
answered on 16 Mar 2010, 09:46 PM
Hi,

Thank you for your reply.
I have tried removing remove the container.Focus() line of code, but it ended up the same. :( I also tried the EditorPrepare event, and added both LostFocus and GotFocus event to the editor, I found that the editor keeping losing and getting focus as LostFocus and GotFocus events fired alternatively. Don't really understand why the editor LostFocus in the first place...any suggestions?

Cheers,

VH
0
Miro Miroslavov
Telerik team
answered on 19 Mar 2010, 12:15 PM
Hello VH,

Can you please find the attached project. When you add new item, it goes to editing mode and you can start editing.
Hope this works for you.

Kind regards,
Miro Miroslavov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
VH
Top achievements
Rank 1
answered on 21 Mar 2010, 11:42 PM
Hi,

Thank you very much for your example. I think I know what caused the problem. I made a TemplateSelector for CellEditTemplate and it somehow stops IsInEditMode from working. I mean, the container was in edit mode when I set IsInEditMode = true, but the focus was not there. I modified your example a bit, so you can have a look. I submmited a support ticket, the ticket ID is: 292515.

Or is there any other ways to make some of the TreeListViewItem not editable rather than using TemplateSelector?

Cheers,

VH
0
Accepted
Miro Miroslavov
Telerik team
answered on 24 Mar 2010, 09:54 AM
Hello VH,

I modified the example with what you need. I'm using DataTemplateSelector as well, but in a different manner. What you also do is to check if you should set InInEditMode = true in the StatusChanged handler (There is comment in the code for that).
Hope this will helps you.

Sincerely yours,
Miro Miroslavov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
VH
Top achievements
Rank 1
answered on 29 Mar 2010, 09:41 PM
Hi,

Thank you very much! That works! :) But still not sure what was the problem with my original approach...

Cheers,

VH
0
Bruno
Top achievements
Rank 1
answered on 13 Dec 2018, 08:12 PM
If the list is virtualized the ItemContainerGenerator.ContainerFromItem returns null. what can we don in such scenario ?
0
Vladimir Stoyanov
Telerik team
answered on 18 Dec 2018, 12:38 PM
Hello Bruno,

Indeed when the control is virtualized, the ContainerFromItem method of the ItemContainerGenerator will not return anything when the item is outside the viewport. What I can suggest is using the ScrollIntoViewAsync method to scroll an item into view and utilize its callback to get the corresponding container.

If this approach does not work for your scenario or you need any further assistance, may I kindly ask you to open a new support ticket and describe your scenario there (since this forum post is a bit outdated) ?

Hope this helps.

Regards,
Vladimir Stoyanov
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
TreeListView
Asked by
VH
Top achievements
Rank 1
Answers by
Miro Miroslavov
Telerik team
VH
Top achievements
Rank 1
Bruno
Top achievements
Rank 1
Vladimir Stoyanov
Telerik team
Share this question
or