using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
namespace LongListSelectorDemo
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
//使用List来初始化数据
List mainItem = new List();
for (int i = 0; i < 10; i++)
{
mainItem.Add(new Item() { Content = "A类别", Title = "测试A " + i.ToString() });
mainItem.Add(new Item() { Content = "B类别", Title = "测试B" + i.ToString() });
mainItem.Add(new Item() { Content = "C类别", Title = "测试C" + i.ToString() });
}
//使用Linq来查询List数据 按照Content来进行分组
var selected = from c in mainItem group c by c.Content into n select new GroupingLayer(n);
this.LongList.ItemsSource = selected;
}
//继承Linq的IGrouping接口 来存储分组的数据
public class GroupingLayer : IGrouping
{
//分组数据
private readonly IGrouping grouping;
//初始化
public GroupingLayer(IGrouping unit)
{
grouping = unit;
}
//唯一的键值
public TKey Key
{
get { return grouping.Key; }
}
//重载判断相等方法
public override bool Equals(object obj)
{
GroupingLayer that = obj as GroupingLayer;
return (that != null) && (this.Key.Equals(that.Key));
}
public IEnumerator GetEnumerator()
{
return grouping.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return grouping.GetEnumerator();
}
}
//List选项的类 Content表示类别 Title表示选项的标题
public class Item
{
public string Title { get; set; }
public string Content { get; set; }
}
}
}
下面再来看看第二例子,实现LongListSelector控件的字母索引。
Item.cs
namespace LongListSelectorDemo
{
///
/// 选项实体类
///
public class Item
{
public string Name { get; set; }
public string Content { get; set; }
//获取名字的首个字符用来作为分组的依据
public static string GetFirstNameKey(Item item)
{
char key;
key = char.ToLower(item.Name[0]);
if (key < 'a' || key > 'z')
{
key = '#';
}
return key.ToString();
}
}
}
ItemInGroup.cs
using System.Collections.Generic;
namespace LongListSelectorDemo
{
///
/// 组集合
///
public class ItemInGroup: List
{
public ItemInGroup(string category)
{
Key = category;
}
//组的键
public string Key { get; set; }
//组是否有选项
public bool HasItems { get { return Count > 0; } }
}
}
Items.cs
using System.Collections.Generic;
namespace LongListSelectorDemo
{
///
/// 总数据集合
///
public class Items: List
{
//索引
private static readonly string Groups = "#|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z";
public Items()
{
//获取要绑定的数据源
List items = new List();
items.Add(new Item { Name = "a测试", Content = "a内容" });
items.Add(new Item { Name = "b测试", Content = "b内容" });
items.Add(new Item { Name = "c测试", Content = "c内容" });
items.Add(new Item { Name = "d测试", Content = "d内容" });
items.Add(new Item { Name = "e测试", Content = "e内容" });
items.Add(new Item { Name = "f测试", Content = "f内容" });
items.Add(new Item { Name = "g测试", Content = "g内容" });
//组的字典列表
Dictionary groups = new Dictionary();
//初始化组列表,即用字母列表来分组
foreach (string c in Groups.Split('|'))
{
ItemInGroup group = new ItemInGroup(c.ToString());
//添加组数据到集合
this.Add(group);
groups[c.ToString()] = group;
}