locked
how do I bind this collection to the TreeView? RRS feed

  • Question

  • given this image, I know the sample model has been created correctly. What do I need to change in the xaml to bind this list of 2 items to the TreeView xaml?  Listed below is the xaml and the model. Thank you!  Greg

     

    <UserControl x:Class="lionbridge.MainPage"
        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"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400"
        xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls"
    	xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls">
    
        <Grid
            x:Name="LayoutRoot"
            Background="White">
            <Grid
                Height="214"
                HorizontalAlignment="Left"
                Margin="43,42,0,0"
                Name="grid1"
                VerticalAlignment="Top"
                Width="324">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition
                        Width="250" />
                    <ColumnDefinition
                        Width="10" />
                    <ColumnDefinition
                        Width="150" />
                </Grid.ColumnDefinitions>
                <controls:TreeView x:Name="treeview" >
                    <controls:TreeView.ItemTemplate>
                        <common:HierarchicalDataTemplate ItemsSource="{Binding Path=Areas}" >
                            <common:HierarchicalDataTemplate.ItemTemplate>
                                <common:HierarchicalDataTemplate ItemsSource="{Binding Path=Locs}" >
                                    <common:HierarchicalDataTemplate.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding Path=LocName}" Foreground="Sienna"  />
                                        </DataTemplate>
                                    </common:HierarchicalDataTemplate.ItemTemplate>
                                    <TextBlock Text="{Binding AreaName}" Foreground="Purple"  />
                                </common:HierarchicalDataTemplate>
                            </common:HierarchicalDataTemplate.ItemTemplate>
                            <TextBlock Text="{Binding CompanyName}" Foreground="Black" FontWeight="Bold"  />
                        </common:HierarchicalDataTemplate>
                    </controls:TreeView.ItemTemplate>
                </controls:TreeView>
    
                <controls:GridSplitter
                    Height="219"
                    Grid.Column="1"
                    HorizontalAlignment="Left"
                    Name="gridSplitter1"
                    VerticalAlignment="Top"
                    BorderThickness="4" />
                <StackPanel
                    x:Name="details"
                    Background="Green"
                    Grid.Column="2">
                </StackPanel>
            </Grid>
        </Grid>
    </UserControl>
    

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    
    namespace lionbridge
    {
    
            public class Company
            {
                public string Name { get; set; }
                public List<Area> Areas { get; set; }
                public List<Area> Children { get { return Areas; } }
            }
        
            public class Area
            {
               public string AreaName { get; set; }
               public string Name { get { return AreaName; } }
               public List<Location> Locs { get; set; }
               public List<Location> Children { get { return Locs; } }
           }
       
           public class Location
           {
               public string Loc { get; set; }
               public string Name { get { return Loc; } }
           }
    }
     
    Friday, February 19, 2010 9:04 AM

Answers

  •  public class Company
        {
            public string Name { get; set; }
            public List<Area> Children { get; set; }    
        }

        public class Area
        {
            public string Name { get; set; }
            public List<Location> Children { get; set; }
        }

        public class Location
        {
            public string Name { get; set; }
        }

     

    <controls:TreeView x:Name="treeview">
       <controls:TreeView.ItemTemplate>   
             <common:HierarchicalDataTemplate ItemsSource="{Binding Children}" >
                  <TextBlock Text="{Binding Name}" Foreground="Black" FontWeight="Bold"  />
              </common:HierarchicalDataTemplate>
       </controls:TreeView.ItemTemplate>
    </controls:TreeView>

     

    List<Company> list = new List<Company>();
                for (int i = 0; i < 10; i++)
                {
                    Company c = new Company { Name = "Company" + "_" + i.ToString()};
                    list.Add(c);
                    c.Children = new List<Area>();
                    for(int j = 0; j<5; j++)
                    {
                        Area a = new Area { Name = "Area" + "_" + i.ToString() + "_" + j.ToString() };
                        c.Children.Add(a);
                        a.Children = new List<Location>();
                        for(int k = 0; k<3; k++)
                            a.Children.Add(new Location { Name = "Loc" + "_"  + i.ToString() + "_" + j.ToString() + "_" + k.ToString() });
                    }
                }
                this.treeview.ItemsSource = list;    

    Friday, February 19, 2010 9:17 PM

All replies

  • Hi,

    maybe something like
     

    ...
    <controls:TreeView x:Name="treeview" 
        ItemsSource={Binding}>
    ...

    ...or the like.


    Cheers,
    Alex

    Friday, February 19, 2010 10:10 AM
  • Here is my blank TreeView looks like given my treeview xaml code below and the same model as above above. I can't figure out why the populated TreeView isn't showing? Thanks for any ideas.....

    <UserControl x:Class="lionbridge.MainPage"
        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"
        mc:Ignorable="d"
        d:DesignHeight="600" d:DesignWidth="800"
        xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls"
    	xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls">
    
        <Grid
            x:Name="LayoutRoot"
            Background="White">
            <Grid
                Height="214"
                HorizontalAlignment="Left"
                Margin="43,42,0,0"
                Name="grid1"
                VerticalAlignment="Top"
                Width="324">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition
                        Width="250" />
                    <ColumnDefinition
                        Width="10" />
                    <ColumnDefinition
                        Width="150" />
                </Grid.ColumnDefinitions>
                <controls:TreeView x:Name="treeview" ItemsSource="Binding"  Grid.Column="0"  >
                    <controls:TreeView.ItemTemplate>
                        <common:HierarchicalDataTemplate ItemsSource="{Binding Path=Areas}" >
                            <common:HierarchicalDataTemplate.ItemTemplate>
                                <common:HierarchicalDataTemplate ItemsSource="{Binding Path=Locs}" >
                                    <common:HierarchicalDataTemplate.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding Path=LocName}" Foreground="Sienna"  />
                                        </DataTemplate>
                                    </common:HierarchicalDataTemplate.ItemTemplate>
                                    <TextBlock Text="{Binding AreaName}" Foreground="Purple"  />
                                </common:HierarchicalDataTemplate>
                            </common:HierarchicalDataTemplate.ItemTemplate>
                            <TextBlock Text="{Binding CompanyName}" Foreground="Black" FontWeight="Bold"  />
                        </common:HierarchicalDataTemplate>
                    </controls:TreeView.ItemTemplate>
                </controls:TreeView>
    
                <controls:GridSplitter
                    Height="219"
                    Grid.Column="1"
                    HorizontalAlignment="Left"
                    Name="gridSplitter1"
                    VerticalAlignment="Top"
                    BorderThickness="4" />
                <StackPanel
                    x:Name="details"
                    Background="Green"
                    Grid.Column="2">
                </StackPanel>
            </Grid>
        </Grid>
    </UserControl>
    
     
    Friday, February 19, 2010 7:24 PM
  •  public class Company
        {
            public string Name { get; set; }
            public List<Area> Children { get; set; }    
        }

        public class Area
        {
            public string Name { get; set; }
            public List<Location> Children { get; set; }
        }

        public class Location
        {
            public string Name { get; set; }
        }

     

    <controls:TreeView x:Name="treeview">
       <controls:TreeView.ItemTemplate>   
             <common:HierarchicalDataTemplate ItemsSource="{Binding Children}" >
                  <TextBlock Text="{Binding Name}" Foreground="Black" FontWeight="Bold"  />
              </common:HierarchicalDataTemplate>
       </controls:TreeView.ItemTemplate>
    </controls:TreeView>

     

    List<Company> list = new List<Company>();
                for (int i = 0; i < 10; i++)
                {
                    Company c = new Company { Name = "Company" + "_" + i.ToString()};
                    list.Add(c);
                    c.Children = new List<Area>();
                    for(int j = 0; j<5; j++)
                    {
                        Area a = new Area { Name = "Area" + "_" + i.ToString() + "_" + j.ToString() };
                        c.Children.Add(a);
                        a.Children = new List<Location>();
                        for(int k = 0; k<3; k++)
                            a.Children.Add(new Location { Name = "Loc" + "_"  + i.ToString() + "_" + j.ToString() + "_" + k.ToString() });
                    }
                }
                this.treeview.ItemsSource = list;    

    Friday, February 19, 2010 9:17 PM
  • Thank you!

    Saturday, February 20, 2010 8:58 AM