Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > TreeView > Add new root row

Not answered Add new root row

Feed from this thread
  • Dmitry Intermediate avatar

    Posted on Jan 31, 2012 (permalink)

    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.

    Reply

  • Petar Mladenov Petar Mladenov admin's avatar

    Posted on Feb 3, 2012 (permalink)

    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 >>

    Reply

  • Dmitry Intermediate avatar

    Posted on Feb 3, 2012 (permalink)

    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.

    Reply

  • Petar Mladenov Petar Mladenov admin's avatar

    Posted on Feb 8, 2012 (permalink)

    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 >>

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > TreeView > Add new root row
Related resources for "Add new root row"

WPF TreeView Features  |  Documentation  |  Demos  |  Telerik TV  |  Self-Paced Trainer  ]