我指的是这里的示例: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<string> subTitle;
public List<string> SubTitle
{
get { return subTitle; }
set { subTitle = value; }
}
}
private static IEnumerable<HighwayCode> GetCityList()
{
return myList;
// Which already contains data:
MainTitle : Chapters
subtitle : ABC
subtitle : X
MainTitle : Chapters Two
subtitle : ASDF
subtitle : GHIJK
}
public class GroupingLayer<TKey, TElement> : IGrouping<TKey, TElement>
{
private readonly IGrouping<TKey, TElement> grouping;
public GroupingLayer(IGrouping<TKey, TElement> unit)
{
grouping = unit;
}
public TKey Key
{
get { return grouping.Key; }
}
public IEnumerator<TElement> GetEnumerator()
{
return grouping.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return grouping.GetEnumerator();
}
}
XAML:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="GroupHeader">
<Border Background="{StaticResource PhoneAccentBrush}" Margin="{StaticResource PhoneTouchTargetOverhang}" Padding="{StaticResource PhoneTouchTargetOverhang}">
<TextBlock Text="{Binding Key}"/>
</Border>
</DataTemplate>
<DataTemplate x:Key="ItemTmpl">
<Grid>
<TextBlock Style="{StaticResource PhoneTextLargeStyle}"
Foreground="Black"
Text="{Binding SubTitle}"></TextBlock>
</Grid>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<phone:LongListSelector x:Name="longListSelector"
IsGroupingEnabled="True" LayoutMode="List" HideEmptyGroups="False"
ItemTemplate="{StaticResource ItemTmpl}"
GroupHeaderTemplate="{StaticResource GroupHeader}"/>
我的设置如下:
var selected = (from c in myList
group c by c.MainTitle into n
select new GroupingLayer<string, MyObject>(n)).ToList();
longListSelector.ItemsSource = selected;
但对我来说,只显示主标题但子标题根本不显示。
这里有什么问题?
答案 0 :(得分:1)
我相信你应该将你的物品来源设置为一个observablecollection
我并没有像你这样做,但here is my xaml和here is a viewmodel为我为Windows手机商店构建的应用程序。
我还认为清除然后在更新时设置项目来源是关键。当我构建一个WPF应用程序时,我似乎记得花费大量时间来解决observablecollection没有更新的问题。
答案 1 :(得分:1)
当你这样做时
var selected = (from c in myList group c by c.MainTitle into n select new GroupingLayer<string, MyObject>(n)).ToList();
你得到一个列表,其中每个项目都有:
当您定义DataTemplate时,您可以绑定属性“Key”,因为此新列表中存在该属性,但SubTitle不存在,因此您无法显示它!
您可以查看此示例:
http://code.msdn.microsoft.com/wpapps/PhotoHub-Windows-Phone-8-fd7a1093