2011-10-02 87 views
3

我正在尋找一種方法將類似於以下應用程序的方式分組。是否有可能使用視圖模型創建組列表框?我打算在具有例如多個客戶羣體:用於Windows Phone 7的組列表框?

「AAA」(集團) - 「XDN」(聯繫)

「NCB」(集團) - 「XDN」(聯繫)

等...我不希望它被字母分開,而是通過組名。這可能嗎?

謝謝。

enter image description here enter image description here

回答

5

沒有什麼能阻止你創建自定義的有序集合適合該精確的目的。下面是我通常使用的收集類型,當它與LongListSelector集成在Silverlight Toolkit

您顯然必須修改GroupHeaderTemplate,但這很容易。使用

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; 
      } 
     } 
    } 
} 

例子:

enter image description here

+0

嘿,冷靜,感謝,這正是我想要的。非常感謝! – Eric

+0

這個github鏈接目前404的:/ –

+0

你介意給一個例子如何使用這個類?謝謝 –