2017-12-13 14 views
0

SQLデータベースに私のアプリケーションtbFlights、tbRegions、Tbcountriesのツリービューに表示したいテーブルが3つあります。フライトは子供でなければならず、異なる地域や国の下で落ちるはずです.................................... ..........................................SQLデータベースからデータを取得し、ツリービュー形式で表示する

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<SefFlight> _flightList = null; 
      public Collection<SefFlight> FlightList 
      { 
       get 
       { 
        return _flightList = _flightList ?? new Collection<SefFlight>(); 
       } 
      } 

      private Collection<Region> _regionList = null; 
      public Collection<Region> RegionList 
      { 
       get 
       { 
        return _regionList = _regionList ?? new Collection<Region>(); 
       } 
      } 

      #region Flight View 

      private bool _showLayout1 = true; 
      public bool ShowLayout1 
      { 
       get 
       { 
        return _showLayout1; 
       } 
       set 
       { 
        _showLayout1 = value; 
       } 
      } 

      //private static List<SefFlight> _sefFlightGroup = null; 
      //public static List<SefFlight> SefFlightGroup 
      //{ 
      // get 
      // { 
      //  return _sefFlightGroup = _sefFlightGroup ?? new List<SefFlight>(); 
      // } 
      //} 

      //private static Dictionary<int, TreeViewItemViewModel> _itemGroupsList = null; 
      //public static Dictionary<int, TreeViewItemViewModel> ItemGroupsList 
      //{ 
      // get 
      // { 
      //  return _itemGroupsList = _itemGroupsList ?? new Dictionary<int, TreeViewItemViewModel>(); 
      // } 
      //} 
      #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<SefFlight>)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<Region>) 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 
     } 
    } 

以下は、自分のデータを表示している私の見解です。

<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"> 
    <UserControl.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary> 
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /> 
       </ResourceDictionary> 
       <ResourceDictionary> 
       </ResourceDictionary> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </UserControl.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="auto" /> 
      <RowDefinition Height="auto"/> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 
     <Ribbon Visibility="{Binding ShowRibbon}"> 
      <Ribbon.ApplicationMenu> 
       <RibbonApplicationMenu Visibility="Collapsed" /> 
      </Ribbon.ApplicationMenu> 
      <RibbonTab Header="Home"> 
       <RibbonGroup Header="Actions"> 
        <RibbonButton LargeImageSource="/Common;component/Resources/home.png" 
           Label="Home" 
           Command="{Binding CloseCommand}"> 
        </RibbonButton> 
       </RibbonGroup> 
       <RibbonGroup Header="Tools"> 
        <RibbonButton LargeImageSource="/Common;component/Resources/Edit.png" 
           Label="Edit" 
           Command="{Binding CloseCommand}"> 
        </RibbonButton> 
       </RibbonGroup> 
      </RibbonTab> 
     </Ribbon> 
     <Grid Grid.Row="1"> 
      <TextBlock Text="Flights" 
         FontSize="25" 
         HorizontalAlignment="Center" /> 
     </Grid> 
     <Grid Grid.Row="2"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Block.LineHeight="45" /> 
       <ColumnDefinition/> 
       <ColumnDefinition/> 
      </Grid.ColumnDefinitions> 

      <Grid.RowDefinitions> 
       <RowDefinition Height="auto"/> 
       <RowDefinition Height="*"/> 
      </Grid.RowDefinitions> 
      <TextBlock Grid.Row="0" 
         Text="Scheduled Flights" 
         HorizontalAlignment="Center"/> 
      <Grid Grid.Row="1"> 
       <GroupBox> 
        <TreeView ItemsSource="{Binding FlightList}"> 
         <TreeView.ItemContainerStyle> 
          <!-- 
    This Style binds a TreeViewItem to a PersonViewModel. 
    --> 
          <Style TargetType="{x:Type TreeViewItem}"> 
           <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /> 
           <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
           <Setter Property="FontWeight" Value="Normal" /> 
           <Style.Triggers> 
            <Trigger Property="IsSelected" Value="True"> 
             <Setter Property="FontWeight" Value="Bold" /> 
            </Trigger> 
           </Style.Triggers> 
          </Style> 
         </TreeView.ItemContainerStyle> 

         <TreeView.ItemTemplate> 
          <HierarchicalDataTemplate ItemsSource="{Binding FlightList}"> 
           <TextBlock Text="{Binding Description}" /> 
          </HierarchicalDataTemplate> 
         </TreeView.ItemTemplate> 
        </TreeView> 
       </GroupBox> 
      </Grid> 
      <TextBlock Grid.Row="0" 
         Grid.Column="1" 
         Text="Scheduled Legs" 
         VerticalAlignment="Top" 
         HorizontalAlignment="Center"/> 
      <Grid Grid.Row="1" 
        Grid.Column="1"> 
       <GroupBox> 
        <TreeView Name="trvList2"> 
         <TreeView.ItemTemplate> 
          <HierarchicalDataTemplate ItemsSource="{Binding FilteredPrincipalList}"> 
           <TextBlock Text="{Binding Title}" /> 
          </HierarchicalDataTemplate> 
         </TreeView.ItemTemplate> 
        </TreeView> 
       </GroupBox> 
      </Grid> 
      <TextBlock Grid.Column="2" 
         Text=" View Scheduled Flights" 
         HorizontalAlignment="Center" 
         /> 
      <Grid Grid.Row="1" 
        Grid.Column="2"> 
       <GroupBox></GroupBox> 
      </Grid> 
     </Grid> 

    </Grid> 






</UserControl> 
+0

Observablecollectionであり、INotifyPropertyChangedを実装し、リストの完了後に変更通知が発生することを確認してください –

+0

それは私がdownvoteではありませんでしたが、あなたがそれを受け取った理由を知っています。私たちはあなたのデータが気にならないと推測しなければなりませんr、あなたのコードは大丈夫です、何が行われるべきかを見ることができます。省略できる部分がありますが、全体的にはこれは問題ありません。 –

答えて

0

FlightListObservablecollectionこと、そしてあなたのリストは、変更通知が発生完了後INotifyPropertyChangedを実施し、確認する必要があります。

私はINotifyPropertyChangedViewModelBaseExtendedクラスであることを考えるだろう、と私はちょうど

ここ

変更があり、それを使用し、それをここに近づかないだろう、それはあなたの問題解決する必要がありますFlightListが必要

private ObservableCollection<SefFlight> _flightList = null; 
public ObservableCollection<SefFlight> FlightList 
{ 
    get 
    { 
     return _flightList ?? new ObservableCollection<SefFlight>(); 
    } 
    set 
    { 
     _flightList = value; 
     OnPropertyChanged(nameof(FlightList)); // implement INotifyPropertyChanged 
    } 

} 

private ObservableCollection<Region> _regionList = null; 
public ObservableCollection<Region> RegionList 
{ 
    get 
    { 
     return _regionList ?? new ObservableCollection<Region>(); 
    } 
    set 
    { 
     _regionList = value; 
     OnPropertyChanged(nameof(RegionList)); // implement INotifyPropertyChanged 
    } 
} 

private void LoadSefFlight() 
{ 
    try 
    { 
     var flightList = new List<SefFlight>(); //   <-- there was change here 
     var result = Common.Helpers.WebRequests.Invoke(ApiInventoryUrls.List_SefFlights, null, null); 

     if (result.IsSuccessful) 
     { 
      foreach (var flight in (List<SefFlight>)result.Result) 
       flightList.Add(flight); //     <-- there was change here 

      FlightList = new ObservableCollection<SefFlight>(flightList); // <-- there was change here 
      LoadRegions(); 
     } 
     else 
      ShowError(result.ErrorMessage); 
    } 
    catch (Exception ex) 
    { 
     ShowError(ex.Message); 
    } 
} 

private void LoadRegions() 
{ 
    try 
    { 
     var regionList = new List<Region>(); //   <-- there was change here 

     var result = Common.Helpers.WebRequests.Invoke(ApiInventoryUrls.List_RegionLists, null, null); 

     if (result.IsSuccessful) 
     { 
      foreach (var region in (List<Region>) result.Result) 
       regionList.Add(region); //     <-- there was change here 

      RegionList = new ObservableCollection<Region>(regionList); //<-- there was change here 
     } 
     else 
      ShowError(result.ErrorMessage); 
    } 
    catch (Exception) 
    { 

     throw; 
    } 
} 
関連する問題