HierarchicalDataTemplate ItemsSource updated is not reflected? (MVVM)

2 posts, 0 answers
  1. Rune
    Rune avatar
    4 posts
    Member since:
    Mar 2011

    Posted 10 Aug 2012 Link to this post

    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

  2. Rune
    Rune avatar
    4 posts
    Member since:
    Mar 2011

    Posted 11 Aug 2012 Link to this post

    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");
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top