无法按照联系人列表在Windows 8.1手机应用程序中按字母顺序对列表视图进行分组

时间:2015-04-12 06:57:50

标签: windows windows-phone-8 windows-phone windows-phone-8.1 windows-8.1

我在我的Pivot项目上有以下列表视图:

<ListView
                ItemsSource="{Binding Items}"
                IsItemClickEnabled="True"
                ContinuumNavigationTransitionInfo.ExitElementContainer="True">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,9.5">
                            <TextBlock
                                Text="{Binding Description}"
                                TextWrapping="Wrap"
                                Pivot.SlideInAnimationGroup="1"
                                CommonNavigationTransitionInfo.IsStaggerElement="True"
                                Style="{ThemeResource ListViewItemTextBlockStyle}"
                                Margin="0,0,19,0"/>
                            <TextBlock
                                Text="{Binding Measure}"
                                TextWrapping="WrapWholeWords"
                                Pivot.SlideInAnimationGroup="2" 
                                CommonNavigationTransitionInfo.IsStaggerElement="True" 
                                Style="{ThemeResource ListViewItemContentTextBlockStyle}"
                                Margin="0,0,19,0"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>

                <ListView.GroupStyle>
                    <GroupStyle >
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Description}" FontWeight="Bold" HorizontalAlignment="Center" />
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                    </GroupStyle>
                </ListView.GroupStyle>

但列表项目没有分组,看起来像:

enter image description here

由于我有超过1600个项目的列表,所以我想按字母顺序对列表项进行分组。因此,当我点击列表视图顶部的“A”框时,手机会带我查看网格中所有字母显示的位置,当我点击特定字母时,手机会将我带回到列表中的项目,其中包含以该字母开头的描述。就像联系人列表视图一样。

1 个答案:

答案 0 :(得分:0)

使用AlphaKeyedList。您可以使用此代码 -

public class AlphaKeyGroup<T>
{
    const string GlobeGroupKey = "\uD83C\uDF10";

    /// <summary>
    /// The Key of this group.
    /// </summary>
    public string Key { get; private set; }

    public List<T> InternalList { get; private set; }

    /// <summary>
    /// Public ctor.
    /// </summary>
    /// <param name="key">The key for this group.</param>
    public AlphaKeyGroup(string key)
    {
        Key = key;
        InternalList = new List<T>();
    }

    /// <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>> CreateDefaultGroups(CharacterGroupings slg)
    {
        List<AlphaKeyGroup<T>> list = new List<AlphaKeyGroup<T>>();

        foreach (CharacterGrouping cg in slg)
        {
            if (cg.Label == "") continue;
            if (cg.Label == "...")
            {
                list.Add(new AlphaKeyGroup<T>(GlobeGroupKey));
            }
            else
            {
                list.Add(new AlphaKeyGroup<T>(cg.Label));
            }
        }

        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, Func<T, string> keySelector, bool sort)
    {
        CharacterGroupings slg = new CharacterGroupings();
        List<AlphaKeyGroup<T>> list = CreateDefaultGroups(slg);

        foreach (T item in items)
        {
            int index = 0;
            {
                string label = slg.Lookup(keySelector(item));
                index = list.FindIndex(alphakeygroup => (alphakeygroup.Key.Equals(label, StringComparison.CurrentCulture)));
            }

            if (index >= 0 && index < list.Count)
            {
                list[index].InternalList.Add(item);
            }
        }

        if (sort)
        {
            foreach (AlphaKeyGroup<T> group in list)
            {
                group.InternalList.Sort((c0, c1) => { return keySelector(c0).CompareTo(keySelector(c0)); });
            }
        }

        return list;
    }
}

}

创建类后,使用LINQ查询对数据进行分组:

    var alphaKeyGroup = AlphaKeyGroup<SampleItem>.CreateGroups(
items,                                      // basic list of items
(SampleItem s) => { return s.Title; },  // the property to sort
 true);                                      // order the items
// returns type List<AlphaKeyGroup<SampleItem>>

将其设置为List视图的ItemsSource,并确保设置组样式..

 <ListView.GroupStyle>
    <GroupStyle HidesIfEmpty="True">
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Background="LightGray" Margin="0" >
                    <TextBlock Text='{Binding Key}'
                       Foreground="Black" Margin="30"
                       Style="{StaticResource HeaderTextBlockStyle}"/>
              </StackPanel>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GroupStyle>
</ListView.GroupStyle>