2011-11-18 60 views
1

我一直在嘗試使用LongListSelector來獲得最基本的分組示例,但只看到我的第一組。LongListSelector只顯示1個組

這是我的XAML:

 <toolkit:LongListSelector x:Name="MainListBox" Background="Transparent" 
      ItemsSource="{Binding Items}"> 
      <toolkit:LongListSelector.GroupHeaderTemplate> 
       <DataTemplate> 
        <Border Background="Transparent"> 
         <Border Background="{StaticResource PhoneAccentBrush}" Width="475" Height="35" HorizontalAlignment="Left"> 
          <TextBlock Text="{Binding Key}" 
             Foreground="{StaticResource PhoneForegroundBrush}" 
             Style="{StaticResource PhoneTextGroupHeaderStyle}" 
             VerticalAlignment="Bottom"/> 
         </Border> 
        </Border> 
       </DataTemplate> 
      </toolkit:LongListSelector.GroupHeaderTemplate> 


      <toolkit:LongListSelector.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Margin="0,0,0,17" Width="432" Orientation="Horizontal"> 
         <TextBlock Text="{Binding name}" TextWrapping="Wrap" Width="345"/> 
        </StackPanel> 
       </DataTemplate> 
      </toolkit:LongListSelector.ItemTemplate> 

     </toolkit:LongListSelector> 

這裏是我的C#代碼:

 List<ItemViewModel> l1 = new List<ItemViewModel>(); 
     List<ItemViewModel> l2= new List<ItemViewModel>(); 
     l1.Add(new ItemViewModel(){ name="test1" }); 
     l1.Add(new ItemViewModel(){ name="test2" }); 
     l2.Add(new ItemViewModel(){ name="test3" }); 
     this.Items.Add(new Group<ItemViewModel>("A", l1)); 
     this.Items.Add(new Group<ItemViewModel>("B", l2)); 

ItemViewModel僅僅是一個容器類。

當我運行代碼,我主要看:

B.


test1的
test2的

,但沒有簽署任何幫助表示讚賞。

組被定義爲:

public class Group<T> : ObservableCollection<T> 
{ 
    public Group(string name, IEnumerable<T> items) 
    { 
     this.Key = name; 
     foreach (T item in items) 
     { 
      this.Add(item); 
     } 
    } 

    public override bool Equals(object obj) 
    { 
     Group<T> that = obj as Group<T>; 

     return (that != null) && (this.Key.Equals(that.Key)); 
    } 

    public string Key 
    { 
     get; 
     set; 
    } 
} 
+0

儘量分別結合在模板中的組屬性。如果你這樣做了,你可以發佈模板嗎? [這裏](http://www.windowsphonegeek.com/articles/wp7-longlistselector-in-depth--part1-visual-structure-and-api)是一個基本的例子,如果這是你第一次嘗試'longlistselector' – abhinav

+0

感謝您的回覆,更新了上面的帖子以包含完整的xaml(希望這就是您所指的)。如前所述,我看到1個GroupHeader和下面的2個項目......只是不是第二組和它的項目。 – user1053873

+0

已解決 - 問題在於xaml中的綁定。而不是這樣做,我需要以編程方式設置MainListBox.ItemsSource。 – user1053873

回答

0

其中T是類型。 的ItemsSource是你的項目的來源,包括T類型 的元素,並指定要排序的元素之後,從您的ItemsSource

List<LonglistSelectorPivot1<T>> DataSource = LonglistSelectorPivot1<T>.CreateGroups(ItemsSource, 
       System.Threading.Thread.CurrentThread.CurrentUICulture, 
       (T s) => { return s.WHAT_YOU_WANNER_SORT_FOR; }, true); 
      MainListBox.ItemsSource = DataSource;