13

MVVMパターンに従ってWPFアプリケーションを作成しています。これで私はエンティティフレームワークを使用しています、

私のエンティティ構造は単純で、部門、コース、本、

部門には多くのコースを含めることができ、コースには多くの本を含めることができます。

これをツリービューで表示したいので、wpfでの出力は次のようになります。

Department1

  Course1

    Book1

    Book2

  Course2

    Book3

Department2

  Course

     Book

Department3   

私のViewModelには、EntityContextオブジェクトがあります。しかし、これをツリービューで表示する方法がわかりません。どうすればこれができますか。

4

4 に答える 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>

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

最後に、データを階層形式で表示します。これで満足していただけると幸いです...

于 2012-06-06T07:35:49.093 に答える
3

このための階層データ テンプレート テンプレートを定義する必要があります。これを使用する方法のサンプルを次に示します。

于 2012-06-06T05:53:28.960 に答える
2

「David Bekham」の回答に基づいて、アイテムを表示するためのより一般的な方法を作成しました

それが役立つホップ:

ジェネリック HierarchicalDataTemplate を定義する

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

ここにビューモデル(コンテンツが静的でない場合は、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)
                    }
                }
            };

誰かがこれが役に立つことを願っています

于 2020-04-07T20:04:49.540 に答える
0

必要なネストされたレベルの HierachialDataTemplate の 'n' レベルを定義する必要があります.これを定義する HierarchicalDataTemplate クラスの ItemsSource プロパティがあります..MenuControl についても同じことができます..

于 2012-06-09T09:17:44.300 に答える