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

TreeView Add Item with Edit Mode

2 Answers 76 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Vincent
Top achievements
Rank 1
Vincent asked on 04 Jul 2011, 08:06 AM
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 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 07 Jul 2011, 07:59 AM
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!

0
Vincent
Top achievements
Rank 1
answered on 07 Jul 2011, 10:13 AM
Thank you.. I was able to get it now! :)
Tags
General Discussions
Asked by
Vincent
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Vincent
Top achievements
Rank 1
Share this question
or