-->

Is it possible to use CollectionView with LongList

2019-07-10 11:26发布

问题:

I am trying to use LongListSelector in conjunction with CollectionView. It seems like CollectionView has all the "stuff" needed, but when I connect the two, nothing happens. Here's what I have so far:

In my control's ctor (the guy that contains the LLS), I construct the CollectionView:

GroupDescription group = new PropertyGroupDescription("FullName", new FirstLetterConverter(true, true));
m_view.GroupDescriptions.Add(group);

Then, whenever the relevant dependency property (the one that contains the list I want to put in the LLS) changes, I go and assign that to m_view and assign the groups to ItemsSource:

private void FriendsChanged()
{
    m_view.Source = Friends;
    friendList.ItemsSource = m_view.View.Groups;

When I inspect the .Groups property, the information seems to be kosher - it has the right amount of items and each item has child-items that seem to be correct. However, it looks like everything is internal and as such the LLS seems to be unable to display the relevant info. When I replace the CollectionView stuff with a mock collection, the LLS does show information (so I am pretty confident that the LLS is set up correctly)

Here's what I am using with the LLS - note that instead of binding, I use "XXXX" in a few places to make sure that nothing is somehow breaking because of faulty binding:

<toolkit:LongListSelector x:Name="friendList">
  <toolkit:LongListSelector.ItemTemplate>
    <DataTemplate>
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="auto"/>
          <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Image Source="{Binding ThumbnailImage}" Width="62" Height="62"/>
        <TextBlock Text="{Binding FullName}"/>
      </Grid>
    </DataTemplate>
  </toolkit:LongListSelector.ItemTemplate>
  <toolkit:LongListSelector.GroupHeaderTemplate>
    <DataTemplate>
      <Border Background="{StaticResource PhoneAccentBrush}" Padding="{StaticResource PhoneTouchTargetOverhang}" Width="200" HorizontalAlignment="Left">
        <!--<TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextLargeStyle}"/>-->
        <TextBlock Text="XXXX" Style="{StaticResource PhoneTextLargeStyle}"/>
      </Border>
    </DataTemplate>
  </toolkit:LongListSelector.GroupHeaderTemplate>
  <toolkit:LongListSelector.GroupItemTemplate>
    <DataTemplate>
      <Border Background="{StaticResource PhoneAccentBrush}" Margin="{StaticResource PhoneTouchTargetOverhang}" Padding="{StaticResource PhoneTouchTargetOverhang}" Width="180">
        <TextBlock Text="XXXX" Style="{StaticResource PhoneTextLargeStyle}"/>
        <!--<TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextLargeStyle}"/>-->
      </Border>
    </DataTemplate>
  </toolkit:LongListSelector.GroupItemTemplate>
</toolkit:LongListSelector>

回答1:

I had the same problem. It seems LongListSelector doesn't work with CollectionView. I solve the issue by binding it to an ObservableCollection instead. I spent two days figuring out why LongListSelector wasn't showing any item. The strange thing is that CollectionView.IsEmpty works for to turn the Visibility (thru a Converter of course). But it doesn't work to show the items.