在列表框中使用ItemSource
本文关键字:ItemSource 列表 | 更新日期: 2023-09-27 18:04:52
我有麻烦得到一个列表绑定到我的ListBox。下面是背后的代码:
using System.Collections.ObjectModel;
using Rocky.Data;
namespace Rocky.UI.Wpf
{
using System.Windows;
internal partial class ShellWindow : Window
{
public ShellWindow()
{
InitializeComponent();
ViewModel = new ShellViewModel();
}
public static ReadOnlyCollection<AuthorInformation> AuthorList
{
get { return AuthorRepository.Authors; }
}
public ShellViewModel ViewModel
{
get { return DataContext as ShellViewModel; }
set { DataContext = value; }
}
}
}
AuthorRepository。Authors只是一个从list创建的ReadOnlyCollection。如果需要,我可以粘贴代码,但我很确定错误不在那个类中。然后是ListBox:
<ListBox
Name="AuthorListBox"
Grid.Row="1"
Margin="3"
VerticalAlignment="Stretch"
Background="Cornsilk"
ItemsSource="{Binding AuthorList}">
</ListBox>
我没有得到一个错误在编译,或在运行时。但是,列表框没有填满。不知道我做错了什么。我在这里看过很多其他的帖子。
AuthorList
属性应该属于ShellViewModel
类