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

Add new root row

3 Answers 61 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Dmitry
Top achievements
Rank 1
Dmitry asked on 31 Jan 2012, 08:42 PM
Hello,
I'm using Entity Framework to provide data to the treeview and I have a tree like this:
<CollectionViewSource x:Key="categoriesViewSource" d:DesignSource="{d:DesignInstance Model:Category, CreateList=True}" />

Assigning DataContext and so on, so everything works fine and correctly displays hierarchical data.

<telerik:RadTreeView HorizontalAlignment="Stretch" IsEditable="True" 
                                     ItemsSource="{Binding}" 
                                     Name="CategoryTreeView" 
                                     VerticalAlignment="Stretch" 
                                         SelectionChanged="CategoryTreeView_OnSelectionChanged" 
                                         KeyUp="CategoryTreeView_KeyUp"
                                         Edited="CategoryTreeView_Edited">
                        <telerik:RadTreeView.ItemEditTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Name, Mode=TwoWay}"/>
                            </DataTemplate>
                        </telerik:RadTreeView.ItemEditTemplate>
                        <telerik:RadTreeView.ItemTemplate>
                            <HierarchicalDataTemplate ItemsSource="{Binding Subcategories}">
                                <TextBlock Text="{Binding Name}" />
                            </HierarchicalDataTemplate>
                        </telerik:RadTreeView.ItemTemplate>
                    </telerik:RadTreeView>

And I want to add root category programmatically. This is what I do:
Category parent = CategoryTreeView.SelectedItem == null 
                    ? null 
                    : _context.Categories.FirstOrDefault(
                            c => c.CategoryID == ((Category) CategoryTreeView.SelectedItem).CategoryID);
           
            Category newCategory = new Category
                                       {
                                           Name = "New category",
                                           ParentCategory = parent,
                                           ParentID = parent == null ? (Guid?) null : parent.CategoryID
                                       };
            _context.Categories.AddObject(newCategory);
Everything also works fine and child items are inserting to selected node. Works until I try to add a root node. When no selected item is available, this code won't work. New category adds to collection but never displays on tree. Can you please help me?
Thank you.

3 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 03 Feb 2012, 05:02 PM
Hi Dmitry ,

 Could you please confirm/check that your DB configuration allows adding a Category with null ParentId / Parent ? Simply check whether this line works successfully:

_context.Categories.AddObject(newCategory);

Do find the category in your DB Tables ? 

Greetings,
Petar Mladenov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Dmitry
Top achievements
Rank 1
answered on 03 Feb 2012, 06:21 PM
Hello,
Yes categories table allows nullable ParentID, there are some root categories there, I had no problem adding them manually to the table. And yes, when I call _context.SaveChanges, new item saved to database. It's just not shown on tree.
0
Petar Mladenov
Telerik team
answered on 08 Feb 2012, 12:51 PM
Hello Dmitry,

 If a ViewModel / business object is properly added to a collection that is bound to RadTreeView and this change is not reflected in the UI, usually it is because this collection does not implement the INotifyCollectionChanged interface. Can you please confirm that your root level collection and your second level collection are both ObservableCollections or collections that implement INotifyCollectionChanged?
If yes, is it possible for you to send use more from your code, this way we would be better able to investigate it ? Thank you in advance for your cooperation.

All the best,
Petar Mladenov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
TreeView
Asked by
Dmitry
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Dmitry
Top achievements
Rank 1
Share this question
or