从SQL数据库中检索数据并以Treeview形式显示

时间:2017-12-13 08:57:42

标签: wpf api

我在SQL数据库中有三个表,我想在我的应用程序,tbFlights,tbRegions,Tbcountries中的treeview中显示。 FLight应该是一个孩子,并且在不同地区和国家之间堕落.................................... ..........................................

    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>

1 个答案:

答案 0 :(得分:0)

FlightList必须是Observablecollection,并实施INotifyPropertyChanged并确保在您的列表完成后发生更改通知。

我会认为INotifyPropertyChangedViewModelBaseExtended课程中,我不会在这里接近它,只是使用它

这里是变化,所以它应该解决你的问题

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;
    }
}