【问题标题】:Displaying Entities in TreeView using MVVM使用 MVVM 在 TreeView 中显示实体
【发布时间】:2012-06-06 05:11:52
【问题描述】:

我正在按照 MVVM 模式制作 WPF 应用程序。在这个我使用实体框架,

我的实体结构很简单,它有3个实体:部门、课程、书籍、

一个系可以有很多课程,一个课程可以有很多书,

现在我想在树视图中显示它,所以我在 wpf 中的输出应该是这样的,

Department1

  Course1

    Book1

    Book2

  Course2

    Book3

Department2

  Course

     Book

Department3   

在我的 ViewModel 中,我有 EntityContext 对象。但我不知道如何在树视图中显示它。 我怎么能做到这一点。

【问题讨论】:

    标签: c# wpf entity-framework mvvm


    【解决方案1】:

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

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

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

    部门类

    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 中没有选定的项目,
    【解决方案2】:

    你必须为这个Here定义层次数据模板模板是如何使用这个的示例。

    【讨论】:

      【解决方案3】:

      根据“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)
                          }
                      }
                  };
      

      我希望有人觉得这很有用

      【讨论】:

        【解决方案4】:

        我们需要为我们想要的嵌套级别定义 HierachialDataTemplate 的“n”级别。我们将使用 HierarchicalDataTemplate 类的 ItemsSource 属性来定义它。我们也可以对 MenuControl 做同样的事情。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-08-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多