Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying Entities in TreeView using MVVM

I am making a WPF application following MVVM pattern. In this i am using entity framework,

my entity structure is simple, it has 3 entities: department, course, books,

a department can have many courses, and a course can have many books,

now i want to show this in a treeview, so my output in wpf should look like this,

Department1

  Course1

    Book1

    Book2

  Course2

    Book3

Department2

  Course

     Book

Department3   

in my ViewModel i have EntityContext object. But i dont know how to show this in a treeview. how i can do this.

like image 971
fhnaseer Avatar asked Jun 06 '12 05:06

fhnaseer


3 Answers

Based on 'David Bekham' answer i created an more generic way to display the items

hops it helps:

define a generic HierarchicalDataTemplate

   <HierarchicalDataTemplate 
      ItemsSource="{Binding ChildItems}" 
      DataType="{x:Type viewModels:TVItemViewModel}">
                   <Label Content="{Binding Name}" />
    </HierarchicalDataTemplate>

here the viewmodel (if your content is non static you need to implement the viewModelbase class )

public class TVItemViewModel 
    {
        private bool isSelected;
        private string name;

        public TVItemViewModel(string name)
        {
            this.Name = name;
        }
        public string Name
        {
            get => name;
            set => name= value;
        }

        public bool IsSelected
        {
            get => isSelected;
            set =>  isSelected= value;
        }

        public ObservableCollection<TVItemViewModel> ChildItems { get; set; }
    }

and in the MainViewModel i create a root collection like

 TvItems = new ObservableCollection<TVItemViewModel>() 
            { new TVItemViewModel("RootItem1") 
                { ChildItems = new ObservableCollection<TVItemViewModel>() 
                    { new TVItemViewModel("Child1"), 
                       new TVItemViewModel("Child2"), 
                        new TVItemViewModel("Child3)
                    }
                }
            };

I hope someone finds this usefull

like image 128
ChrisG Avatar answered Nov 14 '22 13:11

ChrisG


I prepared the small sample to replicate this..

<Window x:Class="TestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:this="clr-namespace:TestApp"
        Title="MainWindow" Height="350" Width="525">

    <Window.DataContext>
        <this:TreeViewModel />
    </Window.DataContext>

    <Window.Resources>

        <HierarchicalDataTemplate ItemsSource="{Binding Courses}" DataType="{x:Type this:Department}">
            <Label Content="{Binding DepartmentName}"/>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate ItemsSource="{Binding Books}" DataType="{x:Type this:Course}">
            <Label Content="{Binding CourseName}"/>
        </HierarchicalDataTemplate>

        <DataTemplate DataType="{x:Type this:Book}">
            <Label Content="{Binding BookName}"/>
        </DataTemplate>

    </Window.Resources>

    <Grid>
        <TreeView ItemsSource="{Binding Departments}">

        </TreeView>
    </Grid>
</Window>

Model and ViewModel classes.

public class Book :ViewModelBase
    {
        private string bookname = string.Empty;

        public string BookName
        {
            get
            {
                return bookname;
            }
            set
            {
                bookname = value;
                OnPropertyChanged("BookName");
            }
        }

        public Book(string bookname)
        {
            BookName = bookname;
        }
    }

Department class

public class Department : ViewModelBase
    {
        private List<Course> courses;

        public Department(string depname)
        {
            DepartmentName = depname;
            Courses = new List<Course>()
            {
                new Course("Course1"),
                new Course("Course2")
            };
        }

        public List<Course> Courses
        {
            get
            {
                return courses;
            }
            set
            {
                courses = value;
                OnPropertyChanged("Courses");
            }
        }

        public string DepartmentName
        {
            get;
            set;
        }
    }

Course class

public class Course :ViewModelBase
    {
        private List<Book> books;

        public Course(string coursename)
        {
            CourseName = coursename;
            Books = new List<Book>()
            {
                new Book("JJJJ"),
                new Book("KKKK"),
                new Book("OOOOO")
            };
        }

        public List<Book> Books
        {
            get
            {
                return books;
            }
            set
            {
                books = value;
                OnPropertyChanged("Books");
            }
        }

        public string CourseName
        {
            get;
            set;
        }
    }

TreeViewModel class.

public class TreeViewModel :ViewModelBase
    {
        private List<Department> departments;

        public TreeViewModel()
        {
            Departments = new List<Department>()
            {
                new Department("Department1"),
                new Department("Department2")
            };
        }

        public List<Department> Departments
        {
            get
            {
                return departments;
            }
            set
            {
                departments = value;
                OnPropertyChanged("Departments");
            }
        }
    }

ViewModelBase class.

public class ViewModelBase :INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propname));
            }
        }
    }

Finally it displays the data in the hierarchical format.. I hope this would satisfy you...

like image 30
David Bekham Avatar answered Nov 14 '22 14:11

David Bekham


You have to define hierarchy data template template for this Here is the sample how to use this.

like image 3
Firoz Avatar answered Nov 14 '22 12:11

Firoz