0

目前我有一個水平包裝的ListBox,但我想將其切換到LongListSelector。除此之外,其原因可能是填充了很多項目,原因是在使用ListBox時,與項目的包裝方式不一致。我希望看到三列,在視圖中顯示項目時需要多行,但根據項目的寬度,使用列表框可以是兩個或三個。該項目包含一個圖像和下面的文本,並且文本(當比圖像更寬時)導致列表中的項目以非均勻的方式包裝。如何將ListBox轉換爲LongListSelector

什麼我目前是

<ListBox Name="ListBoxEffects" SelectionMode="Single" ItemsSource="{Binding}" Margin="{Binding}" 
        toolkit:TiltEffect.IsTiltEnabled="True" SelectionChanged="ListBox_SelectionChanged" > 
       <ListBox.ItemsPanel> 
        <ItemsPanelTemplate> 
         <toolkit:WrapPanel ItemWidth="Auto" /> 
        </ItemsPanelTemplate> 
       </ListBox.ItemsPanel> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Vertical" Margin="12,0,0,24" > 
          <Image Source="{Binding Thumbnail}" Width="128" Height="128" /> 
          <TextBlock Text="{Binding Name}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeNormal}" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

而我試圖做到的是

<phone:LongListSelector Name="ListBoxEffects" Margin="{Binding}" 
            toolkit:TiltEffect.IsTiltEnabled="True" 
            LayoutMode="Grid" GridCellSize="128,128" 
            SelectionChanged="ListBox_SelectionChanged" > 
       <phone:LongListSelector.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Vertical" Margin="12,0,0,24" > 
          <Image Source="{Binding Thumbnail}" Width="128" Height="128" /> 
          <TextBlock Text="{Binding Name}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeNormal}" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
         </StackPanel> 
        </DataTemplate> 
       </phone:LongListSelector.ItemTemplate> 
      </phone:LongListSelector> 

我可以改變原來的<toolkit:WrapPanel ItemWidth="Auto" />到指定的寬度,但我相信,從長遠在動態添加幾個項目的情況下運行,LongListSelector將是更好的選擇。截至目前,沒有任何錯誤,但沒有任何顯示。

+0

嘿,你有沒有解決你的問題的方法,因爲我也面臨同樣的問題。 – shubhi1910

回答

0

你可能想嘗試用LayoutMode="Grid"GridCellSize="250,250",其中250是你定義頁面的任意數量的改變ListBoxPhone:LongListSelector ..

+0

那麼,上面我有我已經改變了'ListBox'到'LongListSelector'所做的更改。我想我的問題是在視圖中沒有任何顯示。我不得不從原始的ListBox中移除ItemsSource =「{Binding}」',但我不知道如何在'LongListSelector'中正確添加它?我相信這是問題。 – Matthew

0
 List<LonglistSelectorPivot1<Class>> DataSource = LonglistSelectorPivot1<Class>.CreateGroups(observableSource, 
      System.Threading.Thread.CurrentThread.CurrentUICulture, 
      (Classs) => { return s.elementToSortAfter; }, true); 
     LongListSelectorName.ItemsSource = DataSource; 

,包括類

public class LonglistSelectorPivot1<T> : List<T> 
{ 
    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 LonglistSelectorPivot1(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<LonglistSelectorPivot1<T>> CreateGroups(SortedLocaleGrouping slg) 
    { 
     List<LonglistSelectorPivot1<T>> list = new List<LonglistSelectorPivot1<T>>(); 

     foreach (string key in slg.GroupDisplayNames) 
     { 
      list.Add(new LonglistSelectorPivot1<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<LonglistSelectorPivot1<T>> CreateGroups(IEnumerable<T> items, CultureInfo ci, GetKeyDelegate getKey, bool sort) 
    { 
     SortedLocaleGrouping slg = new SortedLocaleGrouping(ci); 
     List<LonglistSelectorPivot1<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 (LonglistSelectorPivot1<T> group in list) 
      { 
       group.Sort((c0, c1) => { return ci.CompareInfo.Compare(getKey(c0), getKey(c1)); }); 
      } 
     } 

     return list; 
    } 

} 
相關問題