2012-06-06 39 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也..