Set selected item and get selected container

9 posts, 1 answers
  1. VH
    VH avatar
    27 posts
    Member since:
    Mar 2010

    Posted 10 Mar 2010 Link to this post

    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
  2. Miro Miroslavov
    Admin
    Miro Miroslavov avatar
    588 posts

    Posted 15 Mar 2010 Link to this post

    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.
  3. DevCraft banner
  4. VH
    VH avatar
    27 posts
    Member since:
    Mar 2010

    Posted 15 Mar 2010 Link to this post

    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
  5. Miro Miroslavov
    Admin
    Miro Miroslavov avatar
    588 posts

    Posted 16 Mar 2010 Link to this post

    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.
  6. VH
    VH avatar
    27 posts
    Member since:
    Mar 2010

    Posted 16 Mar 2010 Link to this post

    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
  7. Miro Miroslavov
    Admin
    Miro Miroslavov avatar
    588 posts

    Posted 19 Mar 2010 Link to this post

    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.
  8. VH
    VH avatar
    27 posts
    Member since:
    Mar 2010

    Posted 21 Mar 2010 Link to this post

    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
  9. Answer
    Miro Miroslavov
    Admin
    Miro Miroslavov avatar
    588 posts

    Posted 24 Mar 2010 Link to this post

    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.
  10. VH
    VH avatar
    27 posts
    Member since:
    Mar 2010

    Posted 29 Mar 2010 Link to this post

    Hi,

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

    Cheers,

    VH
Back to Top
DevCraft banner