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

Deleting a child in a Hierarchical Data Template

2 Answers 270 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 27 Sep 2010, 10:35 PM
Hi, I have a database set up with a Client table and a Project table. Clients have projects, and I set up a Hierarchical Data Template to display them in the treeview. I am now putting functionality in for adding/removing both clients and projects, but I've run into an issue.

Adding and removing Clients (the parent nodes) will update the treeview immediately when I call the INotifyPropertyChanged on the ObservableCollection that it is bound to, but the Projects (the children nodes) won't update until I close the application and start it again.

Here is all the relevant code I can think of:

my xaml:
    <Page.Resources>
        <DataTemplate x:Key="Projects" DataType="{x:Type local:Project}">
            <StackPanel Orientation="Horizontal">
                <Image Source="/Images/Project.png" Height="20" Margin="0,0,8,0" />
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
        <HierarchicalDataTemplate x:Key="Clients" DataType="{x:Type local:Client}" ItemTemplate="{StaticResource Projects}" ItemsSource="{Binding Projects}">
            <StackPanel Orientation="Horizontal">
                <Image Source="/Images/Client.png" Height="20" Margin="0,0,2,0" />
                <TextBlock Text="{Binding Name}" />   
            </StackPanel>           
        </HierarchicalDataTemplate>
        <Style x:Key="ItemContainerStyle" TargetType="{x:Type telerik:RadTreeViewItem}">
            <Setter Property="Template" Value="{StaticResource SavanetekTreeItem}"/>
        </Style>
    </Page.Resources>
 
<telerik:RadTreeView x:Name="RadTreeView1" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                         Style="{StaticResource SavanetekTreeViewStyle}" ItemsSource="{Binding ClientList}" SelectedItem="{Binding Path=SelectedObject, Mode=TwoWay}"
                         ItemTemplate="{StaticResource Clients}" ItemContainerStyle="{StaticResource ItemContainerStyle}">
            <telerik:RadTreeView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="New Client..." Click="MenuItemNewClientDetails_Click" />
                    <MenuItem Header="New Project..." Command="{Binding NewProjectCommand}" />
                    <Separator />
                    <MenuItem Header="Edit..." Click="MenuItemEditClientDetails_Click"/>
                    <Separator />
                    <MenuItem Header="Remove..." />
                </ContextMenu>
            </telerik:RadTreeView.ContextMenu>
        </telerik:RadTreeView>


my remove function:

Public Sub Remove()
        If (IsProject(SelectedObject)) Then
            DB.Projects.DeleteOnSubmit(SelectedObject)
            SubmitChangesToDatabase()
        ElseIf (IsClient(SelectedObject)) Then
            DB.Clients.DeleteOnSubmit(SelectedObject)
            SubmitChangesToDatabase()
        Else
 
        End If
 
        RefreshClientList()
    End Sub

And my code for the ClientList with the Refresh and the function to cast the SelectedObject to a Client or a Project:

Private _clientList As ObservableCollection(Of Client)
    Public Property ClientList() As ObservableCollection(Of Client)
        Get
            Return _clientList
        End Get
        Set(ByVal value As ObservableCollection(Of Client))
            _clientList = value
            OnPropertyChanged("ClientList")
        End Set
    End Property
 
Public Sub RefreshClientList()
        ClientList = New ObservableCollection(Of Client)()
        ClientList = ClientRepo.FindAll()
        OnPropertyChanged("ClientList")
    End Sub


I had tried putting a for each loop in the refresh function that listed all the projects in the ClientList, and it does in fact delete/add the Projects to it, so I know that it has the proper data. It just isn't displaying it for some reason.

I would greatly appreciate if anyone knows how to remove a child from a parent on the treeview and have it refresh

2 Answers, 1 is accepted

Sort by
0
Eric
Top achievements
Rank 1
answered on 28 Sep 2010, 12:53 AM
I've tried several different ways of doing this, but they all have failed so far. I just don't get how it knows that there isn't a project there once it's deleted, but won't update the display to show it until i restart the program.
0
Eric
Top achievements
Rank 1
answered on 28 Sep 2010, 01:29 AM
Finally found a way to make it happen, but it's a little sloppy.

I just new up the ViewModel and set it as my DataContext again and it refreshes it.

I was hoping for a more elegant solution, but this works for now.
Tags
TreeView
Asked by
Eric
Top achievements
Rank 1
Answers by
Eric
Top achievements
Rank 1
Share this question
or