LongListSelector grouping, JumpList

504 views Asked by At

I have the LongListSelector that's working as it should, I want only to enable the grouping now. Like it is in the PeopleHub and the JumpList too. How do I do that ? I have checked an example on MSDN but it's complicated and it didn't work for me, maybe I don't understand it right.

I don't fill the LongListSelector with xaml or C# code, but with xml parsing.

First I parse the xml:

XDocument xml = XDocument.Load("xmlfile.xml");

        var data = from query in xml.Descendants("country")
                   select new Country
                   {
                       Name = (string)query.Element("name"),};

and set the itemsSource:

countriesList.ItemsSource = data.ToList();

        // Set the data context of the listbox control to the sample data
        DataContext = App.ViewModel;
    }

I have the Country class:

public class Country
{
    string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }}

Now I would like to group this countries by name. How can I do that ?

Thanks for your help.

2

There are 2 answers

3
McGarnagle On

In the sample they create a fancy-pants helper class called AlphaKeyGroup<T>. Really though, you just need a class to contain each grouping:

public class CountryGrouping : List<Country>
{
    public CountryGrouping(IEnumerable<Country> items) : base(items) { }

    public string Key { get; set; }
}

Bind the ItemsSource to this:

countriesList.ItemsSource = data
    .GroupBy(country => country.Name)
    .Select(grp => new CountryGrouping(grp.ToArray()) { Key = grp.Key })
    .ToList();

I'm guessing that the LongListSelector looks for a property called "Key" as the group header (magic strings!).

Also, don't forget to set IsGroupingEnabled="true" on the control.

2
Depechie On

Take a look at this wiki about the LongListSelector on Nokia Developer site: http://developer.nokia.com/Community/Wiki/LongListSelector_with_bindable_SelectedItem_and_better_scrolling

Because it contains a nice example you can use, but also talks about other things you may be needing if you go further with the LongListSelector ( like getting the selecteditem and other stuff )