LongListSelector don't display ItemSource

本文关键字:display ItemSource don LongListSelector | 更新日期: 2023-09-27 18:06:44

我有一个问题,"新的"longlistselector控件。我从旧的教程,基于工具包:longlistselector。

显示标题但不显示内容:/

Page1.xaml

<phone:LongListSelector Name="GroupedList">
   <phone:LongListSelector.ItemTemplate>
      <DataTemplate>
         <TextBlock Text="{Binding Name}" />
      </DataTemplate>
   </phone:LongListSelector.ItemTemplate>
   <phone:LongListSelector.GroupHeaderTemplate>
      <DataTemplate>
         <TextBlock Text="{Binding Name}" FontSize="32" Foreground="Green" />
      </DataTemplate>
   </phone:LongListSelector.GroupHeaderTemplate>
</phone:LongListSelector>

Page1.xaml.cs

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        if (GroupedList.ItemsSource == null)
        {
            System.Collections.Generic.List<Model.FoodCategory> foodCategories =
                    new System.Collections.Generic.List<Model.FoodCategory>();
            /*---Make burger items---*/
            Model.FoodCategory burgers = new Model.FoodCategory("Burgers");
            burgers.AddFoodItem(new Model.FoodItem("Hamburger"));
            burgers.AddFoodItem(new Model.FoodItem("Chicken burger"));
            burgers.AddFoodItem(new Model.FoodItem("Turkey burger"));
            burgers.AddFoodItem(new Model.FoodItem("Black bean burger"));
            /*---Make fryer items---*/
            Model.FoodCategory fryer = new Model.FoodCategory("Fryer");
            fryer.AddFoodItem(new Model.FoodItem("Fries"));
            fryer.AddFoodItem(new Model.FoodItem("Onion rings"));
            fryer.AddFoodItem(new Model.FoodItem("Tater tots"));
            fryer.AddFoodItem(new Model.FoodItem("Mozzarella sticks"));
            /*---Make fish items---*/
            Model.FoodCategory fish = new Model.FoodCategory("Fish");
            fish.AddFoodItem(new Model.FoodItem("Salmon"));
            fish.AddFoodItem(new Model.FoodItem("Rainbow trout"));
            fish.AddFoodItem(new Model.FoodItem("Grilled tilapia"));
            foodCategories.Add(burgers);
            foodCategories.Add(fryer);
            foodCategories.Add(fish);
            GroupedList.ItemsSource = foodCategories;
        }
    }

模型/FoodItem.cs

class FoodItem
{
    public string Name { get; private set; }
    public FoodItem(string foodName)
    {
        Name = foodName;
    }
}

模型/FoodCategory.cs

class FoodCategory
{
    public string Name { get; private set; }
    public System.Collections.Generic.List<FoodItem> Items { get; private set; }
    public FoodCategory(string categoryName)
    {
        Name = categoryName;
        Items = new System.Collections.Generic.List<FoodItem>();
    }
    public void AddFoodItem(FoodItem foodItem)
    {
        Items.Add(foodItem);
    }
    public System.Collections.IEnumerator GetEnumerator()
    {
        return this.Items.GetEnumerator();
    }
}

LongListSelector don't display ItemSource

尝试将TextBlock放到<ContentControl> </ContentControl>