LongListSelector分组,JumpList

时间:2013-12-17 18:13:57

标签: c# windows-phone-8 longlistselector

我有LongListSelector工作正常,我只想启用分组。就像它在PeopleHub和JumpList中一样。我怎么做 ?我在MSDN上查了一个例子,但它很复杂,但对我来说不起作用,也许我听不懂。

我没有使用xaml或C#代码填充LongListSelector,而是使用xml解析。

首先我解析xml:

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

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

并设置itemsSource:

countriesList.ItemsSource = data.ToList();

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

我有国家课程:

public class Country
{
    string name;

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

现在我想按名称对这些国家进行分组。我怎么能这样做?

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

sample中,他们创建了一个名为AlphaKeyGroup<T>的花式助手类。实际上,你只需要一个类来包含每个分组:

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

    public string Key { get; set; }
}

ItemsSource绑定到此:

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

我猜测LongListSelector寻找名为“Key”的属性作为组头(魔术字符串!)。

另外,不要忘记在控件上设置IsGroupingEnabled="true"

答案 1 :(得分:1)

在诺基亚开发者网站上查看有关LongListSelector的这个wiki:http://developer.nokia.com/Community/Wiki/LongListSelector_with_bindable_SelectedItem_and_better_scrolling

因为它包含一个很好的例子,你可以使用,但是如果你进一步使用LongListSelector(比如获取选择项和其他东西),还会讨论你可能需要的其他东西。