目前我正在尝试编写一个WPF应用程序,它将为虚构的库存储书籍和用户(我需要证明某人是错的)。在我的代码中,我有一个Books类和一个用户类。每个内部都是一个静态列表,可以跟踪它们。我想要做的是列出所有书籍和用户,以便观众可以看到它们。我想我可以使用滚动视图来执行此操作并为其添加存储信息的标签(这将在与主屏幕不同的窗口中,您可以通过按下按钮到达那里)。但是,我在这方面遇到了一些麻烦。
LibraryCore:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibraryCore
{
class User
{
}
class Books
{
public static List<Books> bookslist = new List<Books>();
public static void NewBook(string _title, string _author, string _publisher, int _isbn, int _count = 1)
{
bookslist.Add(new Books(_title, _author, _publisher, _isbn, _count));
}
public static void AddBook(string _title, int _amount)
{
bookslist[bookslist.FindIndex(b => b.Title.ToUpper() == _title.ToUpper())].Count += _amount;
}
public List<Books> currentLoans = new List<Books>();
public string Publisher { get; private set; }
public string Author { get; private set; }
public string Title { get; private set; }
public int ISBN { get; private set; }
public int Count { get; private set; }
Books(string _title, string _author, string _publisher, int _isbn, int _count = 1)
{
Title = _title;
Author = _author;
Publisher = _publisher;
ISBN = _isbn;
Count = _count;
}
}
}
主窗口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LibraryCore;
namespace LibraryLikeWpf
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnNewBook_Click(object sender, RoutedEventArgs e)
{
Books.NewBook("Odessy", "SOME OLD GUY", "Athens Inc.", 0);
//Books.NewBook("OLD YELLER", "SOME OLD GUY", "Athens Inc.", 2);
//Books.NewBook("This old man", "SOME OLD GUY", "Athens Inc.", 1);
}
private void btnInfo_Click(object sender, RoutedEventArgs e)
{
BookList bookList = new BookList();
bookList.Show();
}
}
}
BookListWindow:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using LibraryCore;
namespace LibraryLikeWpf
{
/// <summary>
/// Interaction logic for BookList.xaml
/// </summary>
public partial class BookList : Window
{
public BookList()
{
InitializeComponent();
for(int b = 0; b < Books.bookslist.Count; b++)
{
Label lbl = new Label();
lbl.VerticalAlignment = VerticalAlignment.Top;
lbl.Content = String.Format("Title: {0} Author: {1} Publisher: {2} ISBN: {3}",Books.bookslist[b].Title,Books.bookslist[b].Author, Books.bookslist[b].Publisher, Books.bookslist[b].ISBN);
lbl.Width = 100000;
scrollGrid.Children.Add(lbl);
}
}
}
}
这段代码有效,但如果我添加几本书,BookListWindow将只重叠标签,我想知道如何在实例化时改变它们的位置。此外,即使标签的宽度不应该抑制,标签也会被切断。为什么会发生这种情况,我该如何解决?此外,是否有更好的方法以更好看的方式列出列表中的所有项目?
答案 0 :(得分:0)
如果您正在使用XAML,请尝试将ListView与Binding一起使用。
创建一个ObservableCollection书籍并将其绑定到ListView,这样就可以控制显示你的项目了。