[英]Binding data in 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<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;
但對我來說,它只顯示主標題但子標題根本不顯示。
這有什么不對?
我相信你應該將你的物品來源設置為一個observablecollection
我沒有像你這樣做,但這是我的xaml , 這是我為windows手機商店構建的應用程序的viewmodel 。
我還認為清除然后在更新時設置項目來源是關鍵。 當我構建一個WPF應用程序時,我似乎記得花費大量時間來解決observablecollection沒有更新的麻煩。
當你這樣做
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.