使用 MVVM 在 TreeView 中显示实体 [英] Displaying Entities in TreeView using MVVM
问题描述
我正在按照 MVVM 模式制作 WPF 应用程序.在这个我使用实体框架,
I am making a WPF application following MVVM pattern. In this i am using entity framework,
我的实体结构很简单,它有3个实体:部门、课程、书籍,
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,
现在我想在树视图中显示这个,所以我在 wpf 中的输出应该是这样的,
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
在我的 ViewModel 中,我有 EntityContext 对象.但我不知道如何在树视图中显示它.我怎么能做到这一点.
in my ViewModel i have EntityContext object. But i dont know how to show this in a treeview. how i can do this.
推荐答案
我准备了一个小样本来复制这个..
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 和 ViewModel 类.
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;
}
}
系类
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;
}
}
课程班
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 类.
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 类.
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...
这篇关于使用 MVVM 在 TreeView 中显示实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!