WPF Treeview Binding Problem

Marc Jeeves 386 Reputation points
2020-03-21T22:42:31.933+00:00

Evening,

My Continents are displayed in the treeview in this case, and in the second piece of code my country's are displayed but i cant put both together, can somebody help.

Thanks
Madaxe

                            <TreeView ItemsSource="{Binding ContinentModels}"  Name="Tvw_ContinentsCountrys" Margin="2,2,2,2" Background="#FFFFECEC">
                                <TreeView.ItemTemplate>
                                    <HierarchicalDataTemplate ItemsSource="{Binding continentName}">
                                        <Label Content="{Binding continentName}"/>
                                        <HierarchicalDataTemplate.ItemTemplate>
                                            <HierarchicalDataTemplate ItemsSource="{Binding Countrys}">
                                                <TextBlock Text="{Binding countryName}" />
                                            </HierarchicalDataTemplate>
                                        </HierarchicalDataTemplate.ItemTemplate>
                                    </HierarchicalDataTemplate>
                                </TreeView.ItemTemplate>
                            </TreeView>

<TreeView ItemsSource="{Binding ContinentModels}"  Name="Tvw_ContinentsCountrys" Margin="2,2,2,2" Background="#FFFFECEC">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Countrys}">
            <TextBlock Text="{Binding countryName}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
0 comments No comments
{count} votes

Accepted answer
  1. Marc Jeeves 386 Reputation points
    2020-03-22T14:06:50.537+00:00

    my data context for the treeview is being set on the cs side i moved it to the tree view but im getting an error on the DataType="{x:Type local:Continent}"

    section why?

    Thanks

    Madaxe

                                <TreeView DataContext="_CountryImplementation.ContinentImplementation" ItemsSource="{Binding ContinentModels}"  Name="Tvw_ContinentsCountrys" Margin="2,2,2,2" Background="#FFFFECEC">
    
                                    <TreeView.Resources>
                                        <HierarchicalDataTemplate DataType="{x:Type local:Continent}" ItemsSource="{Binding Countries}">
                                            <TextBlock Text="{Binding ContinentName}"/>
                                        </HierarchicalDataTemplate>
                                    </TreeView.Resources>
    
                                </TreeView>
    

3 additional answers

Sort by: Most helpful
  1. Marc Jeeves 386 Reputation points
    2020-03-22T17:01:07.59+00:00

    That did it thanks for you patience

    <Window x:Class="TestApplication.Tutorial.DataTypesWindow" 
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
               xmlns:Tutorial="clr-namespace:TestApplication.Tutorial"
               xmlns:local="clr-namespace:Infrastructure;assembly=Infrastructure"
               Title="Objects of arbitrary classes" Height="352" Width="239">
    
        <Grid>
            <TreeView ItemsSource="{Binding ContinentModels}"  Name="Tvw_ContinentsCountrys" Margin="2,2,2,2" Background="#FFFFECEC">
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type local:ContinentModel}" ItemsSource="{Binding Countrys}">
                        <TextBlock Text="{Binding ContinentName}"/>
                    </HierarchicalDataTemplate>
                    <HierarchicalDataTemplate DataType="{x:Type local:CountryModel}">
                        <TextBlock Text="{Binding CountryName}"/>
                    </HierarchicalDataTemplate>
                </TreeView.Resources>
            </TreeView>
        </Grid>
    </Window>
    

  2. Peter Fleischer (former MVP) 19,326 Reputation points
    2020-03-22T08:46:53.823+00:00

    Hi, set HierarchicalDataTemplate for each type of node like in following demo.

    XAML:

    <Window x:Class="WpfApp1.Window17"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp17"
            mc:Ignorable="d"
            Title="Window17" Height="450" Width="800">
      <Window.DataContext>
        <local:ViewModel/>
      </Window.DataContext>
        <Grid>
        <TreeView ItemsSource="{Binding ContinentModels}">
          <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:Continent}" ItemsSource="{Binding Countries}">
              <TextBlock Text="{Binding ContinentName}"/>
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:Country}">
              <TextBlock Text="{Binding CountryName}"/>
            </HierarchicalDataTemplate>  
          </TreeView.Resources>
        </TreeView>
        </Grid>
    </Window>
    

    and classes:

    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Data;
    
    namespace WpfApp17
    {
      public class ViewModel
      {
        public ViewModel()
        {
          LoadData();
        }
        CollectionViewSource cvs = new CollectionViewSource();
        private ObservableCollection<Continent> col = new ObservableCollection<Continent>();
        public ICollectionView ContinentModels { get => cvs.View; }
    
        // gerate demo data
        private void LoadData()
        {
          for (int i = 1; i < 10; i++)
          {
            Continent c = new Continent() { ContinentName = $"Continent {i}" };
            col.Add(c);
            for (int k = 1; k < 10; k++) c.Countries.Add(new Country() { CountryName = $"Country {i * 10 + k}" });
          }
          cvs.Source = col;
        }
      }
      public class Continent
      {
        public string ContinentName { get; set; }
        public ObservableCollection<Country> Countries { get; set; } = new ObservableCollection<Country>();
      }
      public class Country
      {
        public string CountryName { get; set; }
      }
    }
    
    0 comments No comments

  3. Marc Jeeves 386 Reputation points
    2020-03-22T13:53:54.683+00:00
    so my solution structure i have at the moment is as follows, so i guess my view model is my implementation.  When i try to replicate your solution i have problems with the DataType="{x:Type local:Continent}" so i don't believe my view model is local?
    thanks
    Madaxe
    Solution
             ImplementationProject
                          ContinentsImplementation
                                    ContinentsObservableCollection
                                              ContinentObject
                                                     ObjectID
                                                     ContinentName
                                                     CountryList
                                                            CountryObject
                                                                   ObjectID
                                                                    CountryName
             InfrastructureProject
                         ModelsFolder
                                    ContinentModel
                                    CountryModel
             UserInterfacesProject
                             WPFUI
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.