Simsons
Simsons

Reputation: 12745

Unable to Display Nested treeView Items

In My View model I have following structure:

class MainWindowViewModel:BaseEntity
{

    #region Output Proprties

    public ObservableCollection<Customer> Customers { get; private set; }

    public ObservableCollection<TreeViewItems> RoorTreeViewItem { get; set; }

    public ObservableCollection<Level2Child> l2Childs { get; set; }

    public ObservableCollection<Level1Child> l1Childs { get; set; }

    public Level1Child l1Child { get; set; }


    #endregion


    public MainWindowViewModel()
    {
        ClickCommand = new RelayCommand(paremer =>
            {
                var customeList = SampleMVVM.Service.Service.GetAllCustomers();
                Customers = new ObservableCollection<Customer>(customeList);


                ObservableCollection<TreeViewItems> tViewIte = new ObservableCollection<TreeViewItems>();

                 l1Childs = new ObservableCollection<Level1Child>();


               l2Childs = new ObservableCollection<Level2Child>();

                Level2Child l2Child1 = new Level2Child { Name = "Zems001", Description = "Zemms as ZemsBond" };
                Level2Child l2Child2 = new Level2Child { Name = "Zems002", Description = "Zemms as ZemsBond" };
                Level2Child l2Child3 = new Level2Child { Name = "Zems003", Description = "Zemms as ZemsBond" };
                Level2Child l2Child4 = new Level2Child { Name = "Zems004", Description = "Zemms as ZemsBond" };
                Level2Child l2Child5 = new Level2Child { Name = "Zems005", Description = "Zemms as ZemsBond" };

                l2Childs.Add(l2Child1);
                l2Childs.Add(l2Child2);
                l2Childs.Add(l2Child3);
                l2Childs.Add(l2Child4);
                l2Childs.Add(l2Child5);




                Level1Child l1Child = new Level1Child { Name = "Bond", Description = "Gems Bond", Level2Child = l2Childs };

                l1Childs.Add(l1Child);

                TreeViewItems rootItem = new TreeViewItems {Name= "Shon Conery",Description= "Octopussy", Level1Child = l1Childs };

                tViewIte.Add(rootItem);

                RoorTreeViewItem = new ObservableCollection<TreeViewItems>(tViewIte);

                NotifyPropertyChanged("Customers");
                NotifyPropertyChanged("RoorTreeViewItem");

            });
    }

    #region InputCommands

   public ICommand ClickCommand { get; private set; }

    #endregion



}

I am trying to show the tree view using XAML:

<Window x:Class="SampleMVVM.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    xmlns:vm="clr-namespace:SampleMVVM.ViewModel"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <!--<HierarchicalDataTemplate DataType = "{x:Type vm:Level1Child}" ItemsSource = "{Binding Path=l1Childs}">
        <TextBlock Text="{Binding Name}"></TextBlock>
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate DataType = "{x:Type vm:Level2Child}" ItemsSource = "{Binding Path=l2Childs}">
        <TextBlock Text="{Binding Name}"></TextBlock>
    </HierarchicalDataTemplate>-->
    <HierarchicalDataTemplate 
        DataType="{x:Type vm:Level1Child}" 
        ItemsSource="{Binding Path=Level2Child, diag:PresentationTraceSources.TraceLevel=High}">
            <TextBlock Text="{Binding Name}"></TextBlock>
    </HierarchicalDataTemplate>
</Window.Resources>
<Grid>

    <TreeView Name="viewsTreeView"  ItemsSource="{Binding RoortTreeViewItem}">

    </TreeView>
    <Button Height="50" Width="100" Content="Get Records" Command="{Binding ClickCommand}" Margin="132,260,285,10"/>
</Grid>

Ideally I should see the tree structure similar to following image, but just getting namespace value as root, nothing else.

Edit

Made following changes, but still getting Top Level elements only:

 <Window.Resources>

        <HierarchicalDataTemplate 
            DataType="{x:Type vm:TreeViewItems}" 
            ItemsSource="{Binding Path=l2Childs, diag:PresentationTraceSources.TraceLevel=High}">
                <TextBlock Text="{Binding Name}"></TextBlock>
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>

        <TreeView Name="viewsTreeView"  ItemsSource="{Binding RootTreeViewItem}"/>

enter image description here

Upvotes: 1

Views: 163

Answers (1)

It would be very helpful if you would include all of your code. I'm left guessing at a lot of stuff that's not shown. There may be further problems in the code that you didn't include.

But to start with, it appears that you are binding the wrong thing to ItemsSource in your templates. You left out your class definitions, but it looks like Level1Child keeps its children in a property called Level2Child. In the template for Level1Child, the DataContext is an instance of Level1Child, not an instance of your viewmodel. So bind to the property on Level1Child:

<HierarchicalDataTemplate 
    DataType="{x:Type vm:Level1Child}" 
    ItemsSource="{Binding Path=Level2Child}"
    >
    <TextBlock Text="{Binding Name}"></TextBlock>
</HierarchicalDataTemplate>

In addition, you have no template at all for your root item type, TreeViewItems. You need a template for that type as well, and as with the others, you need to bind ItemsSource to the correct name of the actual child-collection property on the TreeViewItems class.

You can diagnose these binding issues more easily by adding a trace to a binding that's not doing what you expect. Add the System.Diagnostics namespace to the outermost tag in your XAML file:

<Window
    ...
    xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    ...
    >

And add this attached property to the Binding that's not working as you expect:

diag:PresentationTraceSources.TraceLevel=High

...like so:

<HierarchicalDataTemplate 
    DataType="{x:Type vm:Level1Child}" 
    ItemsSource="{Binding Path=Level2Child, diag:PresentationTraceSources.TraceLevel=High}"
    >
        <TextBlock Text="{Binding Name}"></TextBlock>
</HierarchicalDataTemplate>

In your "Output" pane in Visual Studio at runtime, when it tries to resolve that binding, you'll get a lot of information about what the DataContext actually is, and what the binding is trying to do to resolve itself.

The trace output slows things down, so don't leave it in there permanently. Take it out when you've resolved the problem.

Upvotes: 1

Related Questions