C# 在LongListSelector中绑定数据
我在这里指的是一个例子: 这是我的密码:C# 在LongListSelector中绑定数据,c#,wpf,windows-phone-8,windows-phone,longlistselector,C#,Wpf,Windows Phone 8,Windows Phone,Longlistselector,我在这里指的是一个例子: 这是我的密码: public class Chapters { private string mainTitle; public string MainTitle { get { return mainTitle; } set { mainTitle = value; } } private List<string> s
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();
}
}
公共类章节
{
私有字符串主标题;
公共字符串主标题
{
获取{return mainttitle;}
设置{mainttitle=value;}
}
私人列表字幕;
公开列表字幕
{
获取{return subTitle;}
设置{subTitle=value;}
}
}
私有静态IEnumerable GetCityList()
{
返回myList;
//其中已包含数据:
主要标题:章节
字幕:ABC
字幕:X
标题:第二章
字幕:ASDF
字幕:吉克
}
公共类分组层:I分组
{
私有只读i分组;
公共分组层(I分组单元)
{
分组=单位;
}
公钥
{
获取{return grouping.Key;}
}
公共IEnumerator GetEnumerator()
{
返回grouping.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回grouping.GetEnumerator();
}
}
XAML:
我是这样设置的:
var selected = (from c in myList
group c by c.MainTitle into n
select new GroupingLayer<string, MyObject>(n)).ToList();
longListSelector.ItemsSource = selected;
var selected=(来自myList中的c)
c组由c.MainTitle分为n组
选择new GroupingLayer(n)).ToList();
longListSelector.ItemsSource=已选择;
但对我来说,它只显示主标题,而子标题根本不显示
这里出了什么问题?我认为您应该将项目源设置为observablecollection 我并不像你那样做,但我为windows phone商店构建了一个应用程序 我也相信这是关键,以明确,然后设置您的项目来源的更新。当我构建一个WPF应用程序时,我似乎记得花了很多时间来解决observablecollection无法更新的问题。当您这样做时
var selected=(来自myList中的c)
c组由c.MainTitle分为n组
选择new GroupingLayer(n)).ToList()代码>
您将获得一个列表,其中每个项目都有:
- 一个键属性(在您的示例中包含MainTitle值),因为
按主标题分组李>
- “子”项的列表
当您定义DataTemplate时,您可以绑定属性“Key”,因为它存在于这个新列表中,但SubTitle不存在,所以您不能显示它
您可以查看此示例:
在我的情况下,如何解决这个问题?您能帮助我吗?我需要在这里更改什么?集合需要触发CollectionChanged事件,以便长列表选择器注意更改。这里有一篇博文(昨天碰巧发布)出自一位遇到相同问题的人:您使用了其他集合,如List或IEnumerable,但您需要确保它在加载项目后调用NotifyPropertyChanged。
var selected = (from c in myList
group c by c.MainTitle into n
select new GroupingLayer<string, MyObject>(n)).ToList();
longListSelector.ItemsSource = selected;