none
LongListSelector binding to CollectionViewSource shows empty list

    Question

  • I tried to use CollectionViewSource in my project and when it didn't worked I created a new empty project to simplify things. Now problem is LLS is empty. I checked event handler. e.Accepted actually evaluates to true. May be I miss something.

    What I understand is every Item passed to event handler which Accepted property is true is shown in list, am I right?

    MainPage.xaml:

    <phone:PhoneApplicationPage.Resources>
        <CollectionViewSource x:Name="FilteredList" Source="{Binding Items}" Filter="FilteredList_Filter" />
    </phone:PhoneApplicationPage.Resources>
    
    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
    
        <!--Pivot Control-->
        <phone:Pivot Title="MY APPLICATION">
    
            <!--Pivot item one-->
            <phone:PivotItem Header="first">
                <!--Double line list with text wrapping-->
                <phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding Source={StaticResource FilteredList}}">
                    <phone:LongListSelector.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="0,0,0,17">
                                <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            </StackPanel>
                        </DataTemplate>
                    </phone:LongListSelector.ItemTemplate>
                </phone:LongListSelector>
            </phone:PivotItem>
    
        </phone:Pivot>
    
    </Grid>

    MainPage.xaml.cs:

    namespace Filtered_List_Test
    {
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
    
            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
    
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }
    
        // Load data for the ViewModel Items
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }
    
        private void FilteredList_Filter(object sender, System.Windows.Data.FilterEventArgs e)
        {
            e.Accepted = true;
        }
    }
    }

    Built on standart PivotApp template.

    Saturday, March 15, 2014 9:32 PM

All replies

  • Hi,

    I think in CollectionViewSource you have to use x:Key instead of x:Name. Other than that everything looks fine.

    <CollectionViewSource x:Key="FilteredList" Source="{Binding Items}" Filter="FilteredList_Filter" />
    </phone:PhoneApplicationPage.Resources>


    Pradeep AJ

    Sunday, March 16, 2014 3:48 AM
  • Thanks for the tip Pradeep, but it's not this. 
    Sunday, March 16, 2014 5:04 AM
  • Unfortunately, CollectionViewSource is incompatible with LongListSelector. It will work if you replace LongListSelector with ListBox.

    The reason is that ListBox.ItemsSource is of type IEnumerable, but LongListSelector.ItemsSource is of type IList. CollectionViewSource is implemented to use IEnumerable.


    Richard Woo
    Sunday, March 16, 2014 9:53 PM