0
votes

I have the following classes..

public class Package
{
  public String name { get; set; }
  public List<Class> classList { get; set; }
}

public class Class
{
  public String name { get; set; }
  public List<Method> methodsList { get; set; }
}

public class Method
{
  public String name { get; set; }
}

I am reading the values to these from an xml file. I want to show the values in a treeview. How can i get it done in wpf TreeView?

1
Is this an example, or your actual code? Using keywords as Method and Class, which are already used by C# itself is not really clear when reading the code. Is this of any help? : stackoverflow.com/questions/6494156/… - Mike de Klerk
this is just an example. - niruj

1 Answers

0
votes

I show you what i did with 2 classes in a MVVM project. WARNING : to understand the sample you need to know MVVM (Model View ViewModel) paradigm.

These are the models :

public class Region
{
    public string RegionName { get; set; }
    public ObservableCollection<Cities> ListCities { get; set; }

    public Region()
    {
        this.ListCities = new ObservableCollection<Cities>();
    }
}
public class Cities
{
    public string CityCode { get; set; }
    public string CityName { get; set; }
}

Then the Declarations of observable collections in the ViewModel :

private ObservableCollection<GruppoAperture> _regionGroup;
   public ObservableCollection<GruppoAperture> RegionGroup
   {
      get { return _regionGroup; }
      set
      {
          if (_regionGroup == value)
          return;
          _regionGroup= value;
          RaisePropertyChanged(nameof(RegionGroup));
       }
    }

Then comes the Function that Populate the treeview that you can fire when you need:

private void PopulateTreeView ()
    {
        DataTable regions = //Here you create a datatable of regions

        DataTable cities = // Here you create a datatable of cities

        if (regions != null && cities != null)
        {
            if (regions.Rows.Count > 0)
            {
                foreach (DataRow r in region.Rows)
                {
                    Regions reg = new Regions();
                    reg.RegionName = r["regionName"].ToString();

                    foreach(DataRow r2 in cities.Rows) //assegno aperture
                    {
                        if (r2["regionName"].ToString() == reg.RegionName)
                        {
                            reg.ListCities.Add(new Cities()
                            {
                                CityCode = r2["cityCode"].ToString(),
                                CityName = r2["cityName"].ToString()
                            });
                        }
                    }
                    RegionGroup.Add(reg);
                }
            }
        }
    }

Then the XAML part :

<Window x:Class="MyProject.Views.MainWindow"
        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:MyProject"
        xmlns:local1="clr-namespace:MyProject.ViewModels"
        xmlns:local2="clr-namespace:MyProject.Models"
        mc:Ignorable="d"
        Title="CueModifier" Height="703.448" Width="829.25">
    <Window.DataContext>
        <local1:RegionViewModel></local1:RegionViewModel>
    </Window.DataContext>

<Grid x:Name="regiongrid" Height="538" Margin="10,124,10,0" VerticalAlignment="Top" >
            <TreeView x:Name="treeViewRegions" ItemsSource="{Binding RegionGroup}">
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding ListCities}">
                        <Label Content="{Binding RegionName}"/>
                        <HierarchicalDataTemplate.ItemTemplate>
                            <DataTemplate DataType="{x:Type local2:Cities}">
                                <Label Content="{Binding CityName}"/>
                            </DataTemplate>
                        </HierarchicalDataTemplate.ItemTemplate>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>
        </Grid>