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> 

相關問題