Group Listbox for Windows Phone 7?

后端 未结 1 1325
渐次进展
渐次进展 2021-02-11 01:05

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

1条回答
  •  醉梦人生
    2021-02-11 01:44

    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

    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 : ObservableCollection>
            where T : IComparable
        {
            public LongListCollection()
            {
            }
    
            public LongListCollection(IEnumerable items, Func keySelector)            
            {
                if (items == null)
                    throw new ArgumentException("items");
    
                var groups = new Dictionary>();
    
                foreach (var item in items.OrderBy(x => x))
                {
                    var key = keySelector(item);
    
                    if (groups.ContainsKey(key) == false)
                        groups.Add(key, new LongListItem(key));
    
                    groups[key].Add(item);
                }
    
                foreach (var value in groups.Values)
                    this.Add(value);
            }
        }
    
        public class LongListItem : ObservableCollection
        {
            public LongListItem()
            {
            }
    
            public LongListItem(TKey key)
            {
                this.Key = key;
            }
    
            public TKey Key
            {
                get;
                set;
            }
    
            public bool HasItems
            {
                get
                {
                    return Count > 0;
                }
            }
        }
    }
    

    Example of use:

    enter image description here

    0 讨论(0)
提交回复
热议问题