Set the IsInEditMode after adding new node to RadTreeView

2 posts, 0 answers
  1. Tina
    Tina avatar
    17 posts
    Member since:
    May 2011

    Posted 13 Jun 2011 Link to this post

            I want to select the new node and set it in editing mode, but the ItemContainer is always null.
            I read all the forum posts and How To's but nothing works for me. The new item is selected and visible (it's parent is expanded).
            Also I tried the ExpandItemByPath doesn't do anything on my tree and GetItemByPath returns null.
          
            See the AddItem() method in code below:

    Code behind:

    public partial class MyTreeList
    {
            public MyTreeList()
            {
                InitializeComponent();
                this.DataContext = GenerateList();
                _rootItem = list;
            }
            
            public static ItemVM GenerateList()
            {
                var root = new ItemVM() {Title = "Root"};
                var item1 = new ItemVM() { Title = "Item 1" };
                var item11 = new ItemVM() { Title = "Item 1-1" };
                var item12 = new ItemVM() { Title = "Item 1-2" };
                item1.References.Add(item11);
                item1.References.Add(item12);

                var item2 = new ItemVM() { Title = "Item 2" };
                var item21 = new ItemVM() { Title = "Item 2-1" };
                var item22 = new ItemVM() { Title = "Item 2-2" };
                item2.References.Add(item21);
                item2.References.Add(item22);

                root.References.Add(item1);
                root.References.Add(item2);

                //var collection = new ObservableCollection<ItemVM>() {root};                        
                return root;
            }    
            
            private void AddItem()
            {
                var node = _radTreeView.SelectedContainer;
                var newItem = new ItemVM(){Title = "xxx"};
                AddItem(node, newItem);
            }
            
            private void AddItem(RadTreeViewItem parentContainer, ItemVM newItem)
            {            
                _newItem = (ItemVM)newItem;
                
                if (parentContainer == null)
                {
                    return;
                }
                else
                {            
                    //Add new item to the list - works fine
                    var item = parentContainer.Item as ItemVM;
                    item.References.Add(newItem);
                    parentContainer.IsExpanded = true;
                    
                    // Next want to set the new item into editable mode - nothing works
                    // Element is selected and visible but container is null, programatic expanding doesn't do anything
                    
                    _rootItem.SelectedItem = newItem; - will select new item but doesn't help with the
                    // returns null                
                    var newContainer = _radTreeView.ContainerFromItemRecursive(newItem);
                    //newContainer.IsInEditMode = true;
                    
                    newContainer = _radTreeView.ContainerFromItemRecursive(newItem);
                    //still null
                    newContainer = parentContainer.ItemContainerGenerator.ContainerFromItem(newItem);
                    //still null
                    
                    var oldContainer = _radTreeView.ContainerFromItemRecursive(item);
                                  
                    // Different try (hardcoded path to try it out -- clicked add on "Item 1")              
                    // Expanding doesn't do anything - item is not expanded
                    string path = string.Format("Item 1|xxx");
                    _radTreeView.ExpandItemByPath(path, "|");    
                    var x = _radTreeView.GetItemByPath(path,"|");        //item is null        
                }
            }
    }       
    XAML:
    <UserControl.Resources>
                <telerik:HierarchicalDataTemplate x:Key="Item" ItemsSource="{Binding References}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Title}" />
                    </StackPanel>
                </telerik:HierarchicalDataTemplate>    
            </UserControl.Resources>
            
    <telerik:RadTreeView x:Name="_radTreeView" Margin="10"
                                 ItemsIndent="30"
                                 SelectionMode="Extended"
                                 IsLineEnabled="False"                             
                                 ItemsOptionListType="CheckList"
                                 IsOptionElementsEnabled="True"
                                 IsTriStateMode="True"
                                 IsRootLinesEnabled="True"                                                           
                                 IsExpandOnDblClickEnabled="false"
                                 IsExpandOnSingleClickEnabled="False"
                                 IsDragDropEnabled="True"
                                 ItemTemplate="{StaticResource Item}"
                                 IsEditable="True"
                                 ItemsSource="{Binding Path=References}"
                                 ItemClick="RadTreeView_ItemClick"
                                 ItemDoubleClick="RadTreeView_ItemDoubleClick"
                                 SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                                 >
                <telerik:RadContextMenu.ContextMenu>
                    <telerik:RadContextMenu x:Name="contextMenu" Opened="ContextMenuOpened"
                                            ItemClick="MenuItemClicked"
                                            >
                        <telerik:RadMenuItem Header="Add New Item" />
                        <telerik:RadMenuItem Header="Remove Item" />                    
                    </telerik:RadContextMenu>
                </telerik:RadContextMenu.ContextMenu>
    </telerik:RadTreeView>   

    ViewModel

    public class ItemVM : VMBase
        {
            private string _title;
            public string Title
            {
                get { return _title; }
                set
                {
                    _title = value;
                    NotifyPropertyChanged("Title");
                }
            }
            public IList<ItemVM> _references;
            public IList<ItemVM> References
            {
                get { return _references; }
                set
                {
                    _references = value;
                    NotifyPropertyChanged("References");
                }
            }
        }
  2. Tina
    Tina avatar
    17 posts
    Member since:
    May 2011

    Posted 16 Jun 2011 Link to this post

    I fixed it already. ExpandItemByPath had wrong path. After calling it with the right path it works.
    Seems that it really has to be called no matter if the item is visible or not.
  3. DevCraft banner
Back to Top