gpt4 book ai didi

c# - MVVM TreeView wpf(绑定(bind)?)

转载 作者:太空狗 更新时间:2023-10-29 20:02:55 25 4
gpt4 key购买 nike

抱歉这个菜鸟问题。也许这不是问题 - 但我需要一个教程示例/我正在尝试编写一个模型任务以在 wpf 中使用 mvvm 模式“方便”。我决定写一些类似图书管理员助手的东西。这是一个简单的 wpf 窗口,带有一个组合框(用于选择这本书是奇幻还是科幻)、文本框(用于输入书名)、添加按钮和一个树状 View ,如下所示:

 Books
------>ScienceFic
----->bookName1
----->bookName2
------->Fantasy
------>bookName3

我在将 View 模型与 View 绑定(bind)和分离方面遇到了一些困难。你能帮帮我吗?

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
*/
}

至此,我认为模型已经完成。

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

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

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

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

最佳答案

以下代码是对您的应用程序的快速破解。我还了解到您对 treeview 有问题,所以我只显示代码 passanges 到 treeview 并且我在我的 View 模型的构造函数中填充我的书籍列表以获取一些数据

型号:

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

View 模型:

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>

关于c# - MVVM TreeView wpf(绑定(bind)?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21311858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com