2012-06-06 42 views
12

我正在制作一个遵循MVVM模式的WPF应用程序。在此我使用实体框架,使用MVVM在TreeView中显示实体

我的实体结构简单,它有3个实体:系,当然,书籍,

一个部门可以有很多的课程,一门课程可以有很多书,

现在我想在一个TreeView显示这一点,所以我在WPF输出应该是这样的,

Department1 

    Course1 

    Book1 

    Book2 

    Course2 

    Book3 

Department2 

    Course 

    Book 

Department3 

在我的ViewModel我有对象的EntityContext。但我不知道如何在树形视图中显示。 我该如何做到这一点。

回答

16

予制备的小样本复制此..

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

模型和视图模型类。

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类

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类。

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类。

public class ViewModelBase :INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

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

最后它会显示在分层格式的数据。我希望这将满足你...

+0

现在我想添加选定的项目,我怎么能做到这一点,就像树中的选定项目可以是部门书籍或课程,在列表视图中有SelectedItem,但在hierarchicalDataTemplate中没有选定的项目, –

3

您必须为此定义层次结构数据模板模板Here是如何使用此示例的示例。

0

我们需要定义HierachialDataTemplate的“N”级,因为我们要嵌套水平..我们将有HierarchicalDataTemplate类的ItemsSource属性来定义这个..我们可以做同样的MenuControl也..