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

HierarchicalDataTemplate ItemsSource updated is not reflected? (MVVM)

1 Answer 245 Views
TreeListView
This is a migrated thread and some comments may be shown as answers.
Rune
Top achievements
Rank 1
Rune asked on 10 Aug 2012, 09:41 PM
Hi.

I have a treeview with 3 levels in depth, first time everything looks fine, but when adding to a lower level then the root, it's not reflected.

<telerik:RadTreeView  Margin="8" ItemsSource="{Binding treeRoot}" ItemTemplate="{StaticResource Level1Template}" IsLineEnabled="True" SelectionChanged="JobDesign_SelectionChanged" x:Name="JobDesign"/>

These are my templates
<HierarchicalDataTemplate x:Key="Level3Template">
            <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>
 
        <HierarchicalDataTemplate x:Key="Level2Template" ItemTemplate="{StaticResource Level3Template}" ItemsSource="{Binding Level3}" >
            <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>
 
 
        <HierarchicalDataTemplate x:Key="Level1Template" ItemTemplate="{StaticResource Level2Template}" ItemsSource="{Binding Level2}" >
            <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>


This is my Level1 class

public class Level1
    {
        public Level1()
        {
            this.Level2 = new List<Level2>();
        }
        public string Name
        {
            get;
            set;
        }
        public List<Level2> Level2
        {
            get;
            set;
        }
    }

level 2 and 3 are basicly the same

in the viewmodel file this it treeRoot:
public ObservableCollection<Level1> treeRoot { get; private set; }

when add items directly to treeRoot there is no problem.

But if I try to update in level2 like this:

Level1 jobPlanning = treeRoot.Where(t => t.Name.Equals("Job Planning")).Single();
            jobPlanning.Level2 = new List<Level2>();
 
                Level2 operation = new Level2()
                {
                    Name = "New Item"
                };
this.OnPropertyChanged(() => this.treeRoot);

But the treeview is not updated, what am I missing?
After I have add I can look in treeView and see that the new item has been added, but just not displayed.

Please help me.
Regards
Rune

1 Answer, 1 is accepted

Sort by
0
Rune
Top achievements
Rank 1
answered on 11 Aug 2012, 08:22 AM
Never mind

I figured it out, this was the one that broke the update, instead i'm adding directly into Level2.

jobPlanning.Level2 = new List<Level2>();

Working:
jobPlanning.Level2.Add("New Item");
Tags
TreeListView
Asked by
Rune
Top achievements
Rank 1
Answers by
Rune
Top achievements
Rank 1
Share this question
or