TreeView Add Item with Edit Mode

3 posts, 0 answers
  1. Vincent
    Vincent avatar
    34 posts
    Member since:
    Apr 2011

    Posted 04 Jul 2011 Link to this post

    Hi Telerik!

    I have a code here... 

      private void OnMenuItemClicked(object sender, RoutedEventArgs args)
            {
                RadRoutedEventArgs e = args as RadRoutedEventArgs;
                RadMenuItem item = e.OriginalSource as RadMenuItem;
                RadTreeView it = sender as RadTreeView;
                DataItem tem = (DataItem)it.SelectedItem;
        
                        DataItem itemToAdd = new DataItem()
                        {
                            Text = "New Section",
                            ParentID = tem.ID,
                            Docuname = tem.Docuname,
                            ID = treeitemCollection[treeitemCollection.Count - 1].ID++,
                            RemoveCommand = new DelegateCommand((p) => { treeitemCollection.RemoveItem(p); }),
                            AddCommand = new DelegateCommand((p) => { treeitemCollection.AddItem(p); })
                        };
                        tem.Children.Add(itemToAdd);
                        MessageBox.Show(""+tem.Children.Count());
                        ReportsServiceClient client = new ReportsServiceClient();
                        client.InsertSectionContextAsync("New Section", tem.ID, tem.Docuname);
                        client.CloseAsync();


                        Dispatcher.BeginInvoke(() =>
                        { 
                            RadTreeViewItem currentItem = it.ContainerFromItemRecursive(itemToAdd) as RadTreeViewItem;


                            if (currentItem != null)
                            {
                                currentItem.IsInEditMode = true;
                                currentItem.Focus();
                            }
                            else
                              MessageBox.Show("currentItem is NULL");

                        });

            }   
    This code will add new item in radtreeview with new item in edit mode. It works fine when I add an item if the parent has already a child. But, when I add an item to the parent with no child, a null reference exception occurs. Do you have any idea whats wrong with it?
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    3155 posts

    Posted 07 Jul 2011 Link to this post

    Hi Vincent,

    The ContainerFromItemRecursive will return null if the Container (the RadTreeViewitem that you want to access is not prepared yet, in other word you have to ensure its creation by expanding its parent container. You can do something like so:
    private void RadContextMenu_ItemClick(object sender, RadRoutedEventArgs e)
            {
                RadTreeViewItem container = (sender as RadContextMenu).GetClickedElement<RadTreeViewItem>();
                if (container != null && container.Item is Organization)
                {
                    container.IsExpanded = true;
                    Organization org = container.Item as Organization;
                    Department dept = new Department("new Department to Edit!");
                    org.Departments.Add(dept);
                    Dispatcher.BeginInvoke(() =>
                        {
                            RadTreeViewItem newContainer = treeViewDepartments.ContainerFromItemRecursive(dept);
                            if (newContainer != null)
                            {
                                newContainer.BeginEdit();
                            }
                        });
                }
            }
    The described approach is realized in the attached solution. So please give it a try and let us know if it fits in your scenario.

    Best wishes,
    Petar Mladenov
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  3. Vincent
    Vincent avatar
    34 posts
    Member since:
    Apr 2011

    Posted 07 Jul 2011 Link to this post

    Thank you.. I was able to get it now! :)
Back to Top