Hello,
I'm using Entity Framework to provide data to the treeview and I have a tree like this:
Assigning DataContext and so on, so everything works fine and correctly displays hierarchical data.
And I want to add root category programmatically. This is what I do:
Thank you.
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 == nullEverything 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?
? 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);
Thank you.