0

我有以下我的枢纽项目的ListView组列表视图按字母顺序:无法在Windows 8.1手机应用程序,就像联系人列表

<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”框时,手机会让我查看所有字母在网格中的显示位置,当我点击特定字母手机时,会将我带回列表中以字母开头的列表中的项目。就像联系人列表视图一样。

回答

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

把它设为列表视图的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> 

相关问题