C# · 12月 22, 2021

c# – MVVM treeview wpf(绑定?)

@H_301_1@抱歉没有问题.也许这不是一个问题 – 但我需要一个教程样本/
我正在尝试编写一个模型任务,以便在wpf中使用mvvm模式获得“方便”.我决定写一些像图书管理员的帮手.这是一个简单的wpf窗口,带有一个组合框(用于选择书籍是幻想还是sf),文本框(用于书名输入),按钮添加,以及树状视图,如下所示: Books ——>ScienceFic —–>bookName1 —–>bookName2 ——->Fantasy ——>bookName3

我从视图中看绑定和分离视图模型有一些困难.
你可以帮帮我吗?

public enum LibraryType{ ScienceFic,Fantasy} Class Model: INotifyPropertyChanged { private int bookId; private string bookName; private LibraryType bookType; public event PropertyChangedEventHandler PropertyChanged; //….and for all this fields I have used a INotifyPropertyChanged as /* https://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist */ }

在这一点上,我认为MODEL已经完成了.

现在关于VIEW:
我只需打开MainWindow的编辑器并手动添加按钮,文本框,组合框和树视图.
加入后

combo@R_506_2419@1.ItemsSource = Enum.GetValues(typeof(LibraryType)).Cast<LibraryType>(); combo@R_506_2419@1.SelectedIndex = 0;

我用正确类型的书初始化了我的组合)))但是((我在代码中完成了这个,而不是在XAML中 – 这对于MVVM绑定是否正确?(第一个问题)
接下来,我将我的text@R_506_2419@文本绑定(如我想的那样)到viewmodel中的当前book name属性(我在XAML中这样做了):

<Text@R_506_2419@ Text=”{Binding Path=CurrentBookName}”/>

这是正确的吗? (第二个问题)

最后,viewmodeL

Class viewmodel{ private string currentBookName; private LibraryType selectedBookType; public event PropertyChangedEventHandler PropertyChanged; //also properties and OnPropertyChanged/SetField functions like in Model class //and finally I have created two lists for book storing private List<Model> sfStorage = new List<Model>(); private List<Model> fantasyStorage = new List<Model>(); //and simple getters for this two lists,like: public List<Model> GetSFStorage { get{return this.sfStorage;} } //and a simple function for addition of new books //that I plan to call when AddButton pressed public void Addition(???) { if (!string.IsNullOrEmpty(currentBookName)) { //? I have add CurrentBookName in XAML of View (see above) //?but is it enough to have a property of CurrentBookType //to grant that a valide value from combo@R_506_2419@ will be here? var modelNewBook = new Model(CurrentBookName,CurrentBookType); switch (selectedBookType) { case LibraryType.ScienceFic: sfStorage.Add(modelNewBook); break; case LibraryType.Fantasy: fantasyStorage.Add(modelNewBook); break; default: return; } }}

现在我只是走出成功的一小部分(可能)但是,我怎么能连接
在xaml或代码中使用这两个列表的treeView以及如何调用Addition函数
按下Addbutton时(第三个和主要问题)?
也许,这是noob问题,但我真的需要一个样本.
并且,(或许)这个问题对于试图理解MVVM和绑定的其他人会有所帮助.

解决方法 以下代码是您的应用程序的快速入侵.
我也明白你有树视图的问题,所以我只显示树视图的代码passanges,我填写我的视图模型的构造函数中的书籍列表,以获得一些数据

模型:

internal class Book : NotifyPropertyChangedBase{ private string name; private BookType type; public BookType Type { get => this.type; } set { this.SetProperty( ref this.type,value ); }}public string Name{ get => return this.name; set { this.SetProperty( ref this.name,value ); }}

视图模型:

private readonly List<Book> books;public Mainviewmodel( ){ this.books = new List<Book> { new Book { Name = “Book 1”,Type = BookType.Fantasy },new Book { Name = “Book 2”,new Book { Name = “Book 3”,new Book { Name = “Book 4”,Type = BookType.SciFi } };}public ICollectionView Books{ get { var source = CollectionViewSource.GetDefaultView( this.books ); source.GroupDescriptions.Add( new PropertyGroupDescription( “Type” ) ); return source;}

视图:

<TreeView ItemsSource=”{Binding Books.Groups}”> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource=”{Binding Items}”> <TextBlock Text=”{Binding Name}” /> </HierarchicalDataTemplate> </TreeView.ItemTemplate></TreeView>