Add new root row

4 posts, 0 answers
  1. Dmitry
    Dmitry avatar
    121 posts
    Member since:
    Jul 2010

    Posted 31 Jan 2012 Link to this post

    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.
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 03 Feb 2012 Link to this post

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

  3. UI for WPF is Visual Studio 2017 Ready
  4. Dmitry
    Dmitry avatar
    121 posts
    Member since:
    Jul 2010

    Posted 03 Feb 2012 Link to this post

    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.
  5. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 08 Feb 2012 Link to this post

    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 >>
Back to Top