2012-06-06 11 views
12

Wykonuję aplikację WPF według wzorca MVVM. W ten używam Entity Framework,Wyświetlanie elementów w TreeView przy użyciu MVVM

moja struktura jednostka jest prosta, ma 3 podmioty: dział, oczywiście, książki,

dział może mieć wiele kursów, a kurs może mieć wiele książek,

teraz chcę pokazać to w widoku drzewa, więc moje wyjście w WPF powinien wyglądać tak,

Department1 

    Course1 

    Book1 

    Book2 

    Course2 

    Book3 

Department2 

    Course 

    Book 

Department3 

w moim ViewModel mam obiekt EntityContext. Ale nie wiem, jak to pokazać w widoku drzewa. jak mogę to zrobić.

Odpowiedz

16

Przygotowałem małą próbkę do replikacji tego ..

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

klas modelu i 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; 
     } 
    } 

klasa

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

klasa Kurs

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

klasa TreeViewModel Department.

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

Klasa ViewModelBase.

public class ViewModelBase :INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

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

końcu wyświetla dane w formacie hierarchicznym .. Mam nadzieję, że będzie cię zadowolić ...

+0

teraz chcę dodać wybrany element, jak mogę to zrobić, jak wybrany element w drzewie może być działem książka lub kurs, istnieje SelectedItem w widoku listy, ale w hierarchicalDataTemplate nie ma wybranego elementu, –

3

Musisz zdefiniować szablon szablonu danych hierarchii dla tego Here jest próbką, jak tego użyć.

0

Musimy zdefiniować poziom „n” z HierachialDataTemplate dla zagnieżdżonego poziomu chcemy .. Będziemy mieli właściwość ItemsSource dla klasy HierarchicalDataTemplate, aby to zdefiniować. Możemy zrobić to samo dla MenuControl również ..

Powiązane problemy