MVVM treeview wpf(binding?)



对不起菜鸟问题。也许这不是一个问题 - 但我需要一个教程示例/我正在尝试编写一个模型任务以"方便"使用 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 
/*
http://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist
*/
}

我认为在这一点上,模型已经完成。

现在关于视图: 我只是简单地打开了 MainWindow 的编辑器并手动添加了按钮、文本框、组合框和树视图。 添加后

comboBox1.ItemsSource = Enum.GetValues(typeof(LibraryType)).Cast<LibraryType>();
comboBox1.SelectedIndex = 0;

我已经用正确类型的书籍初始化了我的组合)))但是((,我已经在代码中完成了这一点,而不是在 XAML 中 - 这适用于 MVVM 绑定吗?(第一个问题) 接下来,我将我的文本框文本绑定(正如我所假设的那样)到 ViewModel 中的当前书籍名称属性(我在 XAML 中像这样完成了此操作):

<TextBox Text="{Binding Path=CurrentBookName}"/>

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

最后,视图模型

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 combobox 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 或代码中具有这两个列表 我怎么能调用加法函数 当添加按钮按下时(第三个也是主要问题)? 也许,这是菜鸟问题,但我真的需要一个样本。 而且,(也许)这个问题对其他试图理解 MVVM 和绑定的人会有所帮助。

以下代码是对应用程序的快速破解。 我也明白你在树视图方面有问题,所以我只显示代码密码到树视图,我在视图模型的构造函数中填写我的书籍列表以获得一些数据

型:

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",
Type = BookType.Fantasy
},
new Book
{
Name = "Book 3",
Type = BookType.Fantasy
},
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>

据我了解,您需要帮助使用ViewModelTreeView中生成层次结构。为此,您将使用HierarchicalDataTemplate。您的ViewModel将需要子集合,您将使用 只需将HierarchicalDataTemplateItemsSource属性绑定到该集合。

看看有人做的这个简单的例子:TreeView 和 HierarchicalDataTemplate,逐步

最新更新