Deleting a child in a Hierarchical Data Template

3 posts, 0 answers
  1. Eric
    Eric avatar
    3 posts
    Member since:
    Sep 2010

    Posted 27 Sep 2010 Link to this post

    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. Eric
    Eric avatar
    3 posts
    Member since:
    Sep 2010

    Posted 27 Sep 2010 Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Eric
    Eric avatar
    3 posts
    Member since:
    Sep 2010

    Posted 27 Sep 2010 Link to this post

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