Windows Phone 7 的组列表框? [英] Group Listbox for Windows Phone 7?

查看:23
本文介绍了Windows Phone 7 的组列表框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种对项目进行分组的方法,类似于以下应用程序的分组方式.是否可以使用视图模型创建组列表框?我计划拥有多个客户群,例如:

I'm looking for a way to group items similar to the way the applications below have it for there grouping. Is it possible to create a group listbox using View Models? I plan on having multiple customer groups for example:

AAA"(组)-- "XDN" (联系方式)

"AAA" (Group) -- "XDN" (Contact)

NCB"(组)-- "XDN" (联系方式)

"NCB" (Group) -- "XDN" (Contact)

等...我不希望它由字母分隔,而是由组名分隔.这可能吗?

etc... I don't want it to be seperated by letters but instead by group names. Is this possible?

谢谢.

推荐答案

没有什么可以阻止您创建适合该精确目的的自定义有序集合.这是我在将它与 Silverlight Toolkit<中的 LongListSelector 集成时通常使用的集合类型

Nothing prevents you from creating a custom ordered collection that suits that precise purpose. Here's the collection type I usually use for it, when integrating it with the LongListSelector in the Silverlight Toolkit

您显然必须修改GroupHeaderTemplate,但这很容易.

You'll obviously have to modify the GroupHeaderTemplate but that's very easy.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace LongListSample
{
    public class LongListCollection<T, TKey> : ObservableCollection<LongListItem<T, TKey>>
        where T : IComparable<T>
    {
        public LongListCollection()
        {
        }

        public LongListCollection(IEnumerable<T> items, Func<T, TKey> keySelector)            
        {
            if (items == null)
                throw new ArgumentException("items");

            var groups = new Dictionary<TKey, LongListItem<T, TKey>>();

            foreach (var item in items.OrderBy(x => x))
            {
                var key = keySelector(item);

                if (groups.ContainsKey(key) == false)
                    groups.Add(key, new LongListItem<T, TKey>(key));

                groups[key].Add(item);
            }

            foreach (var value in groups.Values)
                this.Add(value);
        }
    }

    public class LongListItem<T, TKey> : ObservableCollection<T>
    {
        public LongListItem()
        {
        }

        public LongListItem(TKey key)
        {
            this.Key = key;
        }

        public TKey Key
        {
            get;
            set;
        }

        public bool HasItems
        {
            get
            {
                return Count > 0;
            }
        }
    }
}

使用示例:

这篇关于Windows Phone 7 的组列表框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆