question

MarcJeeves-9615 avatar image
0 Votes"
MarcJeeves-9615 asked ·

WPF Treeview Binding Problem

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-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MarcJeeves-9615 avatar image
0 Votes"
MarcJeeves-9615 answered ·

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>



· 4 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, which error?

Have you checked whether the class "Continent" is in the namespace "local"?

0 Votes 0 · ·
MarcJeeves-9615 avatar image MarcJeeves-9615 PeterFleischer-3316 ·

I'm just learning wpf so I'm not sure to be honest

0 Votes 0 · ·

xmlns:local="clr-namespace:InfarstructureProject.Models;assembly=InfarstructureProject" like this?

0 Votes 0 · ·
Show more comments
PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered ·

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; }
   }
 }
· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MarcJeeves-9615 avatar image
0 Votes"
MarcJeeves-9615 answered ·
 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
· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, this structure don't show namespaces and classes! Show class view!

5441-unbenannt.png


0 Votes 0 · ·
unbenannt.png (173.8 KiB)
MarcJeeves-9615 avatar image
0 Votes"
MarcJeeves-9615 answered ·

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>
· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, please, execute my demo without changes and tell us the results.

0 Votes 0 · ·