这是我的列表框的XAML
<ListBox Height="100" Margin="12,12,8,0" Name="CategoriesList" VerticalAlignment="Top">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding Id}" Visibility="Collapsed" />
<TextBox Text="{Binding Title}" FontSize="40" IsReadOnly="True"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是我的班级
public class Categories
{
string Id { get; set; }
string Title { get; set; }
public Categories() { }
public Categories(string value, string text)
{
this.Id = value;
this.Title = text;
}
}
这是带有虚拟数据的分配代码
List<Categories> categoriesList = new List<Categories>();
categoriesList.Add(new Categories("1", "One"));
categoriesList.Add(new Categories("2", "Two"));
categoriesList.Add(new Categories("3", "Three"));
categoriesList.Add(new Categories("4", "Four"));
categoriesList.Add(new Categories("5", "Five"));
CategoriesList.ItemsSource = categoriesList;
列表为空,但项目为5,表示标题未显示...请你帮我解决
答案 0 :(得分:0)
public class Categories
{
public string Id { get; set; }
public string Title { get; set; }
public Categories() { }
public Categories(string value, string text)
{
this.Id = value;
this.Title = text;
}
}
答案 1 :(得分:-1)
您需要将ItemSource
的{{1}}属性设置/绑定到公共类别列表,如下所示:ItemSource = {Binding categoriesList}
判断(无论它居住在哪里):
ListBox
您正在使用本地声明的List变量。
此外,如果您打算以任何方式更改列表内容,那么如果List<Categories> categoriesList = new List<Categories>();
categoriesList.Add(new Categories("1", "One"));
categoriesList.Add(new Categories("2", "Two"));
categoriesList.Add(new Categories("3", "Three"));
categoriesList.Add(new Categories("4", "Four"));
categoriesList.Add(new Categories("5", "Five"));
CategoriesList.ItemsSource = categoriesList;
列表为Categories
,您就会发现更容易。