This question is locked. New answers and comments are not allowed.
Hi,
I'm trying to programmatically delete, add and update items in my RadTreeView.
Here's the XAML for my RadTreeView ("TreeEntity") and data template:
Here is the stub for my data class (an
I assign an object of type List<EntityHierarchy> to my TreeEntity.ItemsSource.
----------------------
Deleting
Here is the code for deleting (Note, here "itemToProcess" is the EntityHierarchy of the RadTreeViewItem being deleted):
For adding, when a new
I would greatly appreciate your help in resolving this.
Thanks!
I'm trying to programmatically delete, add and update items in my RadTreeView.
Here's the XAML for my RadTreeView ("TreeEntity") and data template:
<telerik:HierarchicalDataTemplate x:Key="Entity" ItemsSource="{Binding Entities}" telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}" >
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImageUrl}" />
<TextBlock Text="{Binding Name}" Margin="5, 0, 0, 0" />
</StackPanel><br>
</telerik:HierarchicalDataTemplate><telerikNavigation:RadTreeView x:Name="TreeEntity" ItemTemplate="{StaticResource Entity}"
HorizontalAlignment="Stretch" VerticalAlignment="Top"
SelectionChanged="TreeEntity_SelectionChanged">
</telerikNavigation:RadTreeView>Here is the stub for my data class (an
ObservableCollection):public class EntityHierarchy : ObservableCollection<EntityHierarchy>
{
public EntityHierarchy();
public ToggleState CheckState { get; set; }
public string Code { get; set; }
public string Description { get; set; }
public List<EntityHierarchy> Entities { get; set; }
public string Id { get; set; }
public string ImageUrl { get; set; }
public bool IsExpanded { get; set; }
public bool IsSelected { get; set; }
public string Name { get; set; }
public int NodeLevel { get; set; }
public string Path { get; set; }
public int SortOrder { get; set; }
public string TypeId { get; set; }
}I assign an object of type List<EntityHierarchy> to my TreeEntity.ItemsSource.
----------------------
Deleting
Here is the code for deleting (Note, here "itemToProcess" is the EntityHierarchy of the RadTreeViewItem being deleted):
EntityHierarchy parent = ParentItem.DataContext as EntityHierarchy;<br>ParentItem.IsExpanded = false;
for (int i = 0; i < parent.Entities.Count; i++)
{
if (parent.Entities[i].Id.Equals((itemToProcess as EntityHierarchy).Id, StringComparison.CurrentCultureIgnoreCase))
{
parent.Entities.RemoveAt(i);
break;
}
}
ParentItem.IsExpanded = true;
Problem with deleting:
When debugging, I see that the correct EntityHierarchy is removed from the parent.Entities, but in the UI the last child of the parent is deleted and the EntityHierarchy that was removed programmatically is still visible (deleted from underlying collection but not correctly picked up by the UI?).
----------------------
Updating/Adding
Here is the code for updating/adding (Note, here "itemToProcess" is the EntityHierarchy of the Parent RadTreeViewItem of the EntityHierarchy item I am updating/adding, and the new/updated EntityHierarchy is retrieved by generateEntityFromPrimaryInstrument()):
RadTreeViewItem item = TreeEntity.ContainerFromItemRecursive(itemToProcess as EntityHierarchy);
if (item != null && item is RadTreeViewItem)
{
EntityHierarchy parent = item.DataContext as EntityHierarchy;
EntityRaw entity = generateEntityFromPrimaryInstrument(saveWindow.PrimaryInstrumentToSave);
if (entity != null)
{
List<EntityRaw> EntityRaws = new List<EntityRaw>();
EntityRaws.Add(entity);
List<EntityHierarchy> newItem = BuildSubTree(parent.Id, 0, ref EntityRaws);
if (newItem != null && newItem.Count > 0)
{
item.IsExpanded = false;
EntityHierarchy oldItem = null;
foreach (EntityHierarchy child in parent.Entities)
if (child.Id.Equals(newItem[0].Id, StringComparison.CurrentCultureIgnoreCase))
oldItem = child;
if (oldItem != null)
{
oldItem.Name = newItem[0].Name;
oldItem.Description = newItem[0].Description;
oldItem.NodeLevel = newItem[0].NodeLevel;
oldItem.SortOrder = newItem[0].SortOrder;
oldItem.Code = newItem[0].Code;
oldItem.Path = newItem[0].Path;
}
else
(item.Item as EntityHierarchy).Entities.Add(newItem[0]);
//parent.Entities.Sort(CompareEntityHierarchiesBySortOrder);
item.IsExpanded = true;
return;
}
}
}Problem with updating/adding:
For updating, say I've changed the EntityHierarchy's Name property, the change doesn't show in the UI (still shows the old name).For adding, when a new
EntityHierarchy is added, expanding the parent takes up more space but no text or icon is visible for the new item, as if there's just a blank line that got added (it's not even selectable).I would greatly appreciate your help in resolving this.
Thanks!