使用MVVM模式在TreeView中显示实体

13

我正在按照MVVM模式制作一个WPF应用程序。在这个应用程序中,我使用了Entity Framework,

我的实体结构很简单,它有三个实体:department(部门)、course(课程)、books(书籍),

一个部门可以拥有多个课程,一个课程可以拥有多本书籍,

现在我想在TreeView中显示这些内容,所以我的WPF输出应该看起来像这样,

Department1

  Course1

    Book1

    Book2

  Course2

    Book3

Department2

  Course

     Book

Department3   

在我的ViewModel中,我有一个EntityContext对象。但是我不知道如何在TreeView中显示它。我该怎么做呢?

4个回答

17

我准备了一个小样本来复制这个问题..

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

部门类

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

最后,它以分层格式显示数据。我希望这能满足您的需求...


现在我想添加选定的项目,我该怎么做呢?比如树中选择的项目可以是部门、书籍或课程,在列表视图中有SelectedItem,但在HierarchicalDataTemplate中没有选定的项目。 - fhnaseer

3
您需要为此定义层次数据模板模板。这里是一个示例,展示了如何使用它:Here

2

基于'David Bekham'的答案,我创建了一种更通用的方法来显示项目

希望这可以帮到您:

定义一个通用的HierarchicalDataTemplate

   <HierarchicalDataTemplate 
      ItemsSource="{Binding ChildItems}" 
      DataType="{x:Type viewModels:TVItemViewModel}">
                   <Label Content="{Binding Name}" />
    </HierarchicalDataTemplate>

在这里,ViewModel(如果您的内容是非静态的,则需要实现ViewModelBase类)

public class TVItemViewModel 
    {
        private bool isSelected;
        private string name;

        public TVItemViewModel(string name)
        {
            this.Name = name;
        }
        public string Name
        {
            get => name;
            set => name= value;
        }

        public bool IsSelected
        {
            get => isSelected;
            set =>  isSelected= value;
        }

        public ObservableCollection<TVItemViewModel> ChildItems { get; set; }
    }

在MainViewModel中,我创建了一个根集合,如下所示:
 TvItems = new ObservableCollection<TVItemViewModel>() 
            { new TVItemViewModel("RootItem1") 
                { ChildItems = new ObservableCollection<TVItemViewModel>() 
                    { new TVItemViewModel("Child1"), 
                       new TVItemViewModel("Child2"), 
                        new TVItemViewModel("Child3)
                    }
                }
            };

我希望有人能从中受益。

0
我们需要为我们想要的嵌套级别定义“n”级HierachialDataTemplate。我们将使用HierarchicalDataTemplate类的ItemsSource属性来定义此属性。我们也可以对MenuControl执行相同的操作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接