LongListSelector只显示1个组

本文关键字:1个组 显示 LongListSelector | 更新日期: 2023-09-27 18:15:11

我一直在尝试使用LongListSelector获得分组工作的最基本示例,但我只看到我的第一个组。

我的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只是一个容器类。

当我运行代码时,我基本上看到:


test1
test2

但没有b的迹象

任何帮助都是感激的。

组定义为:

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

LongListSelector只显示1个组

其中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;