C# 列表框不显示数据

C# 列表框不显示数据,c#,windows-phone-7,listbox,C#,Windows Phone 7,Listbox,这是我的列表框的XAML <ListBox Height="100" Margin="12,12,8,0" Name="CategoriesList" VerticalAlignment="Top"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBox Text="

这是我的列表框的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>
这是带有虚拟数据的分配代码

        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;
List categoriesList=newlist();
添加(新类别(“1”、“1”);
添加(新类别(“2”、“2”);
添加(新类别(“3”、“3”);
添加(新类别(“4”、“4”);
添加(新类别(“5”、“5”);
CategoriesList.ItemsSource=CategoriesList;

列表为空,但项目为5,这意味着不显示标题…您可以帮助我吗

您需要将
列表框的
ItemSource
属性设置/绑定到一个公共类别列表,如下ItemSource={Binding categoriesList}

由此判断(无论它在哪里):

List categoriesList=newlist();
添加(新类别(“1”、“1”);
添加(新类别(“2”、“2”);
添加(新类别(“3”、“3”);
添加(新类别(“4”、“4”);
添加(新类别(“5”、“5”);
CategoriesList.ItemsSource=CategoriesList;
您正在使用一个本地声明的列表变量


此外,如果您打算以任何方式更改列表内容,那么如果您的
类别
列表是一个
可观察集合

,那么您会发现事情会变得更容易,正如@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;
    }
}

在WP7中没有组合框吗??:(Silverlight Toolkit(第三方组合框)中有一个ListPicker)将您的字段公开。这是绑定的一个要求-1:他确实将
ItemsSource
设置为他的列表…:
CategoriesList.ItemsSource=CategoriesList;
我的意思是绑定到公开列表根本不是问题。如果您直接将变量分配给
ItemsSource
,那么它是否是局部变量。
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;
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;
    }
}