ListBox不显示数据

时间:2011-12-01 10:29:51

标签: c# windows-phone-7 listbox

这是我的列表框的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,表示标题未显示...请你帮我解决

2 个答案:

答案 0 :(得分:0)

正如@ Ku6opr所说的那样,变量公开使列表运行...感谢他

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,您就会发现更容易。