Windows Phone 8 SDK question using the LongListSelector to group on dates.
I am familiar with the AlphaKeyGroup helper approach to grouping on letters.
Has anyone done/seen a similar write up for dates that is similarly locale aware? (Numbers would be a plus as well)
So I struggled with this one a bit too because the AlphaKeyGroup example from MSDN you mentioned is more complicated than it needs to be because of localization. What you are trying to do is create a new List object that that has one extra property, the Key. This Key property is the name that you group on. In the AlphaKeyGroup example it is each letter of the alphabet in your region. So create your own group object that inherits from List.
public class TimeKeyGroup<T> : List<T>
{
/// <summary>
/// The Key of this group.
/// </summary>
public string Key { get; private set; }
public TimeKeyGroup(string key)
{
Key = key;
}
}
Now create a method called CreateGroups that accepts an IEnumerable of the object you want to group and returns a list of you custom list object that you just created. In my implementation I was grouping Workout objects that had a TimeStamp property. In this method create group objects for each type of group key name you want such as "Last 7 Day" or "Last 6 Months". Then fill each group by loooping over the passed in IEnumerable group and evaluating each to determine where they should be grouped. Finally add each grouped list to a master group list and return it. Here is my method:
public static List<TimeKeyGroup<Workout>> CreateGroups(IEnumerable<Workout> workouts)
{
// Create List to hold each item
List<TimeKeyGroup<Workout>> groupedWorkouts = new List<TimeKeyGroup<Workout>>();
// Create a TimeKeyGroup for each group I want
TimeKeyGroup<Workout> LastSeven = new TimeKeyGroup<Workout>("Last Seven Days");
TimeKeyGroup<Workout> LastTwoWeeks = new TimeKeyGroup<Workout>("Last Two Weeks");
TimeKeyGroup<Workout> LastMonth = new TimeKeyGroup<Workout>("Last Month");
TimeKeyGroup<Workout> LastSixMonths = new TimeKeyGroup<Workout>("Last Six Months");
TimeKeyGroup<Workout> LastYear = new TimeKeyGroup<Workout>("Last Year");
TimeKeyGroup<Workout> AllTime = new TimeKeyGroup<Workout>("All Time");
// Fill each list with the appropriate workouts
foreach (Workout w in workouts)
{
if (w.TimeStamp > DateTime.Now.AddDays(-7))
{
LastSeven.Add(w);
continue;
}
else if (w.TimeStamp > DateTime.Now.AddDays(-14))
{
LastTwoWeeks.Add(w);
continue;
}
else if (w.TimeStamp > DateTime.Now.AddMonths(-1))
{
LastMonth.Add(w);
continue;
}
else if (w.TimeStamp > DateTime.Now.AddMonths(-6))
{
LastSixMonths.Add(w);
continue;
}
else if (w.TimeStamp > DateTime.Now.AddMonths(-12))
{
LastYear.Add(w);
continue;
}
else
{
AllTime.Add(w);
}
}
// Add each TimeKeyGroup to the overall list
groupedWorkouts.Add(LastSeven);
groupedWorkouts.Add(LastTwoWeeks);
groupedWorkouts.Add(LastMonth);
groupedWorkouts.Add(LastSixMonths);
groupedWorkouts.Add(LastYear);
groupedWorkouts.Add(AllTime);
return groupedWorkouts;
}
Now you have a nice list of grouped lists. Awesome! The rest is just hooking the itemssource property of your LongListSelector to this new list and defining a JumpListStyle and GroupedHeaderTemplate. The original article you referenced has all that info.
Good luck and happy Windows Phone Development!
I've had success with this example from MSDN after I was stuck on the same example as you are now. The Group.cs file contains an implementation of a group which can be freely used with strings. My guess is, that you could easily add another property of DateTime and then you could try grouping by dates.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With