Skip to content

Instantly share code, notes, and snippets.

@mcnemesis
Created August 27, 2013 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcnemesis/6351001 to your computer and use it in GitHub Desktop.
Save mcnemesis/6351001 to your computer and use it in GitHub Desktop.
This class allows us to be able to create custom groups, with custom keys, that can be easily consumed by jump lists and used to group items in long list selector controls for Windows Phone 8. Jump-Style Yey!
using System;
using System.Collections.Generic;
using System.Linq;
namespace LongListSelectorUtils
{
/// <summary>
/// This class allows us to be able to create custom groups, with custom keys,
/// that can be easily consumed by jumplists and used to group items in longlist selector controls.
///
/// Usage is simple:
/// List<MyClass> itemsList = items as List<MyClass>;
/// myLongListControl.ItemSource = CustomKeyGroup<MyClass>.GetGroups(itemsList,(MyClass c) => c.DesiredKeyProperty);
///
/// Then in your XAML templates (GroupHeaderTemplate and JumpListStyle),
/// bind the necessary Text attributes to the Group property 'Key'
///
/// Simple, right?
/// </summary>
/// <typeparam name="T"></typeparam>
public class CustomKeyGroup<T> : List<T>
{
public static List<Group<T>> GetGroups(List<T> items, Func<T,string> getKeyFunc)
{
IEnumerable<T> List = items;
return GetItemGroups(List, getKeyFunc);
}
public static List<Group<T>> GetItemGroups<T>(IEnumerable<T> itemList, Func<T, string> getKeyFunc)
{
IEnumerable<Group<T>> groupList = from item in itemList
group item by getKeyFunc(item) into g
orderby g.Key
select new Group<T>(g.Key, g);
return groupList.ToList();
}
public class Group<T> : List<T>
{
public Group(string name, IEnumerable<T> items)
: base(items)
{
this.Key = name;
}
public string Key { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment