2012-06-06 13 views
12

Sto creando un'applicazione WPF seguendo lo schema MVVM. In questo sto utilizzando Entity Framework,Visualizzazione di entità in TreeView tramite MVVM

mia struttura entità è semplice, dispone di 3 soggetti: reparto, naturalmente, libri,

un reparto può avere molti corsi, e un corso può avere molti libri,

ora voglio mostrare questo in una vista ad albero, così la mia uscita in WPF dovrebbe assomigliare a questo,

Department1 

    Course1 

    Book1 

    Book2 

    Course2 

    Book3 

Department2 

    Course 

    Book 

Department3 

nel mio ViewModel ho oggetto EntityContext. Ma non so come mostrare questo in una treeview. come posso fare questo.

risposta

16

Ho preparato il piccolo campione di replicare questo ..

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

classi del modello e ViewModel.

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

classe Dipartimento

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

Classe di corso

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

classe 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"); 
      } 
     } 
    } 

Classe ViewModelBase.

public class ViewModelBase :INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

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

Infine visualizza i dati nel formato gerarchico .. Spero che questo sarebbe soddisfarvi ...

+0

ora voglio aggiungere elemento selezionato, come posso farlo, come elemento selezionato in albero può essere dipartimento libro o corso, c'è SelectedItem nella visualizzazione elenco, ma in hierarchicalDataTemplate non c'è nessun elemento selezionato, –

3

È necessario definire il modello di modello di dati di gerarchia per questo Here è l'esempio su come utilizzare questo.

0

Abbiamo bisogno di definire il livello di 'n' di HierachialDataTemplate per il livello nidificato che vogliamo .. Avremo la proprietà ItemsSource per la classe HierarchicalDataTemplate per definire questo .. Possiamo fare lo stesso anche per MenuControl ..

Problemi correlati