具有字符串组标头的LongListSelector

本文关键字:LongListSelector 字符串 | 更新日期: 2023-09-27 18:21:40

我有这个类AlphaKeyGroup:

public class AlphaKeyGroup<T> : List<T>
{
    /// <summary>
    /// The delegate that is used to get the key information.
    /// </summary>
    /// <param name="item">An object of type T</param>
    /// <returns>The key value to use for this object</returns>
    public delegate string GetKeyDelegate(T item);
    /// <summary>
    /// The Key of this group.
    /// </summary>
    public string Key { get; private set; }
    /// <summary>
    /// Public constructor.
    /// </summary>
    /// <param name="key">The key for this group.</param>
    public AlphaKeyGroup(string key)
    {
        Key = key;
    }
    /// <summary>
    /// Create a list of AlphaGroup<T> with keys set by a SortedLocaleGrouping.
    /// </summary>
    /// <param name="slg">The </param>
    /// <returns>Theitems source for a LongListSelector</returns>
    private static List<AlphaKeyGroup<T>> CreateGroups(SortedLocaleGrouping slg)
    {
        List<AlphaKeyGroup<T>> list = new List<AlphaKeyGroup<T>>();
        foreach (string key in slg.GroupDisplayNames)
        {
            list.Add(new AlphaKeyGroup<T>(key));
        }
        return list;
    }
    /// <summary>
    /// Create a list of AlphaGroup<T> with keys set by a SortedLocaleGrouping.
    /// </summary>
    /// <param name="items">The items to place in the groups.</param>
    /// <param name="ci">The CultureInfo to group and sort by.</param>
    /// <param name="getKey">A delegate to get the key from an item.</param>
    /// <param name="sort">Will sort the data if true.</param>
    /// <returns>An items source for a LongListSelector</returns>
    public static List<AlphaKeyGroup<T>> CreateGroups(IEnumerable<T> items, CultureInfo ci, GetKeyDelegate getKey, bool sort)
    {
        SortedLocaleGrouping slg = new SortedLocaleGrouping(ci);
        List<AlphaKeyGroup<T>> list = CreateGroups(slg);
        foreach (T item in items)
        {
            int index = 0;
            if (slg.SupportsPhonetics)
            {
                //check if your database has yomi string for item
                //if it does not, then do you want to generate Yomi or ask the user for this item.
                //index = slg.GetGroupIndex(getKey(Yomiof(item)));
            }
            else
            {
                index = slg.GetGroupIndex(getKey(item));
            }
            if (index >= 0 && index < list.Count)
            {
                list[index].Add(item);
            }
        }
        if (sort)
        {
            foreach (AlphaKeyGroup<T> group in list)
            {
                group.Sort((c0, c1) => { return ci.CompareInfo.Compare(getKey(c0), getKey(c1)); });
            }
        }
        return list;
    }
}

这就是我创建LongListSelector项目的方式来源:

foreach (string item in playlistRep.playlistDic.Keys)
{
     playlistCollection.Add(new PlaylistItem(item, playlistRep.playlistDic[item].Count));
}
List<AlphaKeyGroup<PlaylistItem>> DataSource = AlphaKeyGroup<PlaylistItem>.CreateGroups(playlistCollection,
            System.Threading.Thread.CurrentThread.CurrentUICulture,
            (PlaylistItem s) => { return s.Name; }, true);
playlistList.ItemsSource = DataSource;

我有一个LongListSelector,上面有这样的键:

"A"
a...
a...
a....
"B"
"C"
c.....
c..
c..
........

我想知道我应该怎么做才能获得这样的组名:

"Playlist"
bla bla bla
"Vimeo Playlist"
bla
bla
bla

我希望能够用字符串而不是字符创建一个标题

具有字符串组标头的LongListSelector

定义一个类似的类

    public class PlaylistItem
    {
        public string ItemName { get; set; }
        public string PlayListGroup { get; set; }
    }
    public MainPage()
    {
        InitializeComponent();
        List<PlaylistItem> PlayList = new List<PlaylistItem>();
        //Add your playlistitems in PlayList
        PlayList.Add(new PlaylistItem() { ItemName = "abc", PlayListGroup = "Vimeo Playlist" });
        PlayList.Add(new PlaylistItem() { ItemName = "ab", PlayListGroup = "Playlist" });
        var groupedPlayList =
                from list in PlayList
                group list by list.PlayListGroup into listByGroup
                select new KeyedList<string, PlaylistItem>(listByGroup);
        LongListSelectorList.ItemsSource = new List<KeyedList<string, PlaylistItem>>(groupedPlayList);
    }

现在将KeyedList定义为

    public class KeyedList<TKey, TItem> : List<TItem>
    {
        public TKey Key { protected set; get; }
        public KeyedList(TKey key, IEnumerable<TItem> items)
        : base(items)
        {
            Key = key;
        }
        public KeyedList(IGrouping<TKey, TItem> grouping)
        : base(grouping)
        {
            Key = grouping.Key;
        }
    }

记住:在groupHeader模板中,将TextBlock文本绑定为

    <TextBlock Text="{Binding Key}" /> 
相关文章:
  • 没有找到相关文章