title: Retrieving data from SQL database and Display in Treeview form
body: <p>I have three tables in SQL database which I would like to display in treeview in my App, tbFlights, tbRegions, Tbcountries. The FLight should be a child and the fall under different regions and countries..............................................................................</p>
  
  <pre><code>    using Admin_Tool.Classes;
      using Common.Classes;
      using Common.Enums;
      using System;
      using System.Collections.Generic;
      using System.Collections.ObjectModel;
  
      namespace Admin_Tool.ViewModels
      {
          public class ViewFlightsViewModel : ViewModelBaseExtended
          {
              #region Properties
  
              private Collection&lt;SefFlight&gt; _flightList = null;
              public Collection&lt;SefFlight&gt; FlightList
              {
                  get
                  {
                      return _flightList = _flightList ?? new Collection&lt;SefFlight&gt;();
                  }
              }
  
              private Collection&lt;Region&gt; _regionList = null;
              public Collection&lt;Region&gt; RegionList
              {
                  get
                  {
                      return _regionList = _regionList ?? new Collection&lt;Region&gt;();
                  }
              }
  
              #region Flight View
  
              private bool _showLayout1 = true;
              public bool ShowLayout1
              {
                  get
                  {
                      return _showLayout1;
                  }
                  set
                  {
                      _showLayout1 = value;
                  }
              }
  
              //private static List&lt;SefFlight&gt; _sefFlightGroup = null;
              //public static List&lt;SefFlight&gt; SefFlightGroup
              //{
              //    get
              //    {
              //        return _sefFlightGroup = _sefFlightGroup ?? new List&lt;SefFlight&gt;();
              //    }
              //}
  
              //private static Dictionary&lt;int, TreeViewItemViewModel&gt; _itemGroupsList = null;
              //public static Dictionary&lt;int, TreeViewItemViewModel&gt; ItemGroupsList
              //{
              //    get
              //    {
              //        return _itemGroupsList = _itemGroupsList ?? new Dictionary&lt;int, TreeViewItemViewModel&gt;();
              //    }
              //}
              #endregion
  
              #region 
  
              #endregion
  
              #endregion
  
              #region Constructors
  
              public ViewFlightsViewModel(MainViewModel parent) : base (parent)
              {
                  LoadSefFlight();
                  LoadRegions();
              }
              #endregion
  
              #region Methods
  
              private void LoadSefFlight()
              {
                  try
                  {
                      FlightList.Clear();
  
                      var result = Common.Helpers.WebRequests.Invoke(ApiInventoryUrls.List_SefFlights, null, null);
  
                      if (result.IsSuccessful)
                      {
                          foreach (var flight in (List&lt;SefFlight&gt;)result.Result)
                              FlightList.Add(flight);
                          LoadRegions();
                      }
                      else
                          ShowError(result.ErrorMessage);
                  }
                  catch (Exception ex)
                  {
                      ShowError(ex.Message);
                  }
              }
  
              private void LoadRegions()
              {
                  try
                  {
                      RegionList.Clear();
  
                      var result = Common.Helpers.WebRequests.Invoke(ApiInventoryUrls.List_RegionLists, null, null);
  
                      if (result.IsSuccessful)
                      {
                          foreach (var region in (List&lt;Region&gt;) result.Result)
                              RegionList.Add(region);
                      }
                      else
                          ShowError(result.ErrorMessage);
                  }
                  catch (Exception)
                  {
  
                      throw;
                  }
              }
              #endregion
  
  
              #region Overrides
  
              public override void Refresh()
              {
                  throw new NotImplementedException();
              }
              #endregion
  
              #region Relay Commands
  
              #endregion
  
              #region Commands
  
              #endregion
          }
      }
  </code></pre>
  
  <p>The below is my view where I am displaying my data.</p>
  
  <pre><code>&lt;UserControl x:Class="Admin_Tool.Views.ViewFlightsView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:local="clr-namespace:Admin_Tool.Views"
               mc:Ignorable="d" 
               d:DesignHeight="300" d:DesignWidth="300"&gt;
      &lt;UserControl.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;ResourceDictionary.MergedDictionaries&gt;
                  &lt;ResourceDictionary&gt;
                      &lt;BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /&gt;
                  &lt;/ResourceDictionary&gt;
                  &lt;ResourceDictionary&gt;
                  &lt;/ResourceDictionary&gt;
              &lt;/ResourceDictionary.MergedDictionaries&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/UserControl.Resources&gt;
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="auto" /&gt;
              &lt;RowDefinition Height="auto"/&gt;
              &lt;RowDefinition Height="*" /&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Ribbon Visibility="{Binding ShowRibbon}"&gt;
              &lt;Ribbon.ApplicationMenu&gt;
                  &lt;RibbonApplicationMenu Visibility="Collapsed" /&gt;
              &lt;/Ribbon.ApplicationMenu&gt;
              &lt;RibbonTab Header="Home"&gt;
                  &lt;RibbonGroup Header="Actions"&gt;
                      &lt;RibbonButton LargeImageSource="/Common;component/Resources/home.png"
                                  Label="Home"
                                  Command="{Binding CloseCommand}"&gt;
                      &lt;/RibbonButton&gt;
                  &lt;/RibbonGroup&gt;
                  &lt;RibbonGroup Header="Tools"&gt;
                      &lt;RibbonButton LargeImageSource="/Common;component/Resources/Edit.png"
                                  Label="Edit"
                                  Command="{Binding CloseCommand}"&gt;
                      &lt;/RibbonButton&gt;
                  &lt;/RibbonGroup&gt;
              &lt;/RibbonTab&gt;
          &lt;/Ribbon&gt;
          &lt;Grid Grid.Row="1"&gt;
              &lt;TextBlock Text="Flights" 
                         FontSize="25" 
                         HorizontalAlignment="Center" /&gt;
          &lt;/Grid&gt;
          &lt;Grid Grid.Row="2"&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Block.LineHeight="45" /&gt;
                  &lt;ColumnDefinition/&gt;
                  &lt;ColumnDefinition/&gt;
              &lt;/Grid.ColumnDefinitions&gt;
  
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="auto"/&gt;
                  &lt;RowDefinition Height="*"/&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;TextBlock Grid.Row="0" 
                         Text="Scheduled Flights"
                         HorizontalAlignment="Center"/&gt;
              &lt;Grid Grid.Row="1"&gt;
                  &lt;GroupBox&gt;
                      &lt;TreeView ItemsSource="{Binding FlightList}"&gt;
                          &lt;TreeView.ItemContainerStyle&gt;
                              &lt;!-- 
      This Style binds a TreeViewItem to a PersonViewModel. 
      --&gt;
                              &lt;Style TargetType="{x:Type TreeViewItem}"&gt;
                                  &lt;Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /&gt;
                                  &lt;Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /&gt;
                                  &lt;Setter Property="FontWeight" Value="Normal" /&gt;
                                  &lt;Style.Triggers&gt;
                                      &lt;Trigger Property="IsSelected" Value="True"&gt;
                                          &lt;Setter Property="FontWeight" Value="Bold" /&gt;
                                      &lt;/Trigger&gt;
                                  &lt;/Style.Triggers&gt;
                              &lt;/Style&gt;
                          &lt;/TreeView.ItemContainerStyle&gt;
  
                          &lt;TreeView.ItemTemplate&gt;
                              &lt;HierarchicalDataTemplate ItemsSource="{Binding FlightList}"&gt;
                                  &lt;TextBlock Text="{Binding Description}" /&gt;
                              &lt;/HierarchicalDataTemplate&gt;
                          &lt;/TreeView.ItemTemplate&gt;
                      &lt;/TreeView&gt;
                  &lt;/GroupBox&gt;
              &lt;/Grid&gt;
              &lt;TextBlock Grid.Row="0"
                         Grid.Column="1"
                         Text="Scheduled Legs" 
                         VerticalAlignment="Top"
                         HorizontalAlignment="Center"/&gt;
              &lt;Grid Grid.Row="1" 
                    Grid.Column="1"&gt;
                  &lt;GroupBox&gt;
                      &lt;TreeView Name="trvList2"&gt;
                          &lt;TreeView.ItemTemplate&gt;
                              &lt;HierarchicalDataTemplate ItemsSource="{Binding FilteredPrincipalList}"&gt;
                                  &lt;TextBlock Text="{Binding Title}" /&gt;
                              &lt;/HierarchicalDataTemplate&gt;
                          &lt;/TreeView.ItemTemplate&gt;
                      &lt;/TreeView&gt;
                  &lt;/GroupBox&gt;
              &lt;/Grid&gt;
              &lt;TextBlock Grid.Column="2" 
                         Text=" View Scheduled Flights"
                         HorizontalAlignment="Center"
                         /&gt;
              &lt;Grid Grid.Row="1" 
                    Grid.Column="2"&gt;
                  &lt;GroupBox&gt;&lt;/GroupBox&gt;
              &lt;/Grid&gt;
          &lt;/Grid&gt;
  
      &lt;/Grid&gt;
  
  
  
  
  
  
  &lt;/UserControl&gt;
  </code></pre>