The correct data binding to display the hierarchy

Денис Шумков 21 Reputation points
2020-12-09T10:32:16.227+00:00

Hi, everybody. I apologize a thousand times for my superficial and possibly trivial question, but I can't figure out how it works. What I need to get:
I need to display information about the Department and who it reports to in a hierarchical way in the app. What I did for this:
I Defined the class and implemented the interface:

 public class Departaments : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;


        private string managementPositionName;


        private string whoReportsToTheDepartment;


        public string ManagementPositionName
        {
            get
            {
                return managementPositionName;
            }
            set
            {
                managementPositionName = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(managementPositionName)));
            }
        }


        public string WhoReportsToTheDepartment
        {
            get
            {
                return whoReportsToTheDepartment;
            }
            set
            {
                whoReportsToTheDepartment = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(WhoReportsToTheDepartment)));
            }
        }

       public Departaments(string formalManagePositionName, string formalWhoReportsToTheDepartment)
        {
            ManagementPositionName = formalManagePositionName;
            WhoReportsToTheDepartment = formalWhoReportsToTheDepartment;
        }
    }

Described the xaml markup:

<TreeView Grid.Row="1" Name="DepartamentHierarchy">
       <TreeViewItem Header="{Binding WhoReportsToTheDepartment}">
               <TreeViewItem Header="{Binding ManagementPositionName}"/>
        </TreeViewItem>
</TreeView>

Ideally, I need to create something similar to observablecollection. But all my attempts to link such a collection end in failure. The most I could do:

DataContext = new Departaments("3", "4");

As it is clear, only this line will be shown.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-12-09T15:35:40.62+00:00

    HI,
    try following demo:

    XAML:

    <Window x:Class="WpfApp1.Window022"  
            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:WpfApp022"  
            mc:Ignorable="d"  
            Title="Window022" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
      <Grid>  
        <TreeView ItemsSource="{Binding View}">  
          <TreeView.Resources>  
            <HierarchicalDataTemplate DataType="{x:Type local:Departament}"   
                                      ItemsSource="{Binding WhoReportsToTheDepartment}">  
                <TextBlock Text="{Binding Name}"/>  
            </HierarchicalDataTemplate>  
            <HierarchicalDataTemplate DataType="{x:Type local:Report}">  
              <TextBlock Text="{Binding ReportName}"/>  
            </HierarchicalDataTemplate>  
          </TreeView.Resources>  
        </TreeView>  
      </Grid>  
    </Window>  
    

    And code:

    using System.Collections.ObjectModel;  
    using System.ComponentModel;  
    using System.Windows;  
    using System.Windows.Data;  
      
    namespace WpfApp022  
    {  
      public class ViewModel  
      {  
        public ViewModel()  
        {  
          for (int i = 1; i < 10; i++)  
          {  
            Departament dp = new Departament() { Name = $"ManagementPosition {i}" };  
            col.Add(dp);  
            for (int k = 1; k < 5; k++) dp.WhoReportsToTheDepartment.Add(new Report() { ReportName = $"Report {k}" });  
          }  
          cvs.Source = col;  
        }  
      
        public ICollectionView View { get => cvs.View; }  
        private CollectionViewSource cvs = new CollectionViewSource();  
        private ObservableCollection<Departament> col = new ObservableCollection<Departament>();  
      }  
      
      public class Departament  
      {  
        public string Name { get; set; }  
        public ObservableCollection<Report> WhoReportsToTheDepartment { get; set; } = new ObservableCollection<Report>();  
      }  
      
      public class Report  
      {  
        public string ReportName { get; set; }  
      }  
    }  
    

    Result:

    46610-x.png

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful