在LongListSelector中绑定数据

我指的是这里的例子: http : //dotnet.dzone.com/articles/using-longlistselector-control

这是我的代码:

public class Chapters { private string mainTitle; public string MainTitle { get { return mainTitle; } set { mainTitle = value; } } private List subTitle; public List SubTitle { get { return subTitle; } set { subTitle = value; } } } private static IEnumerable GetCityList() { return myList; // Which already contains data: MainTitle : Chapters subtitle : ABC subtitle : X MainTitle : Chapters Two subtitle : ASDF subtitle : GHIJK } public class GroupingLayer : IGrouping { private readonly IGrouping grouping; public GroupingLayer(IGrouping unit) { grouping = unit; } public TKey Key { get { return grouping.Key; } } public IEnumerator GetEnumerator() { return grouping.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return grouping.GetEnumerator(); } } 

XAML:

              

我这样设置:

 var selected = (from c in myList group c by c.MainTitle into n select new GroupingLayer(n)).ToList(); longListSelector.ItemsSource = selected; 

但对我来说,它只显示主标题但子标题根本不显示。

这有什么不对?

我相信你应该将你的物品来源设置为一个observablecollection

我没有像你这样做,但这是我的xaml , 这是我为windows手机商店构建的应用程序的viewmodel 。

我还认为清除然后在更新时设置项目来源是关键。 当我构建一个WPF应用程序时,我似乎记得花了很多时间来解决observablecollection没有更新的麻烦。

当你这样做

var selected = (from c in myList group c by c.MainTitle into n select new GroupingLayer(n)).ToList();

你得到一个每个项目都有的列表:

  • 一个Key属性(在您的情况下包含MainTitle值),因为您按MainTitle分组!
  • “子”项列表

当您定义DataTemplate时,可以绑定属性“Key”,因为存在于此新列表中,但SubTitle不存在,因此您无法显示它!

你可以看一下这个样本:

http://code.msdn.microsoft.com/wpapps/PhotoHub-Windows-Phone-8-fd7a1093