I am trying to develop a page in a WP8 app which has a search box at the top and a list of items that match the search below, updating as the search box updates. Pretty straightforward, right? Except I can't get it to work.
After perusing StackOverflow and the interwebz, the recommended solution seems to be to use CollectionViewSource. Well, I'm trying to use it and no items show up. As soon as I switch to the ObservableCollection containing my items in XAML, everything shows up fine.
My data is loaded asynchronously from a DB.
XAML
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<toolkit:PhoneTextBox Grid.Row="0" Hint="search query" ActionIcon="/Assets/Search.png" Text="{Binding SearchQuery, Mode=TwoWay}">
<i:Interaction.Behaviors>
<themes:TextBoxUpdateOnTextChangedBehavior />
</i:Interaction.Behaviors>
</toolkit:PhoneTextBox>
<phone:LongListSelector Grid.Row="1" ItemsSource="{Binding SearchResults}" />
<!-- I have also tried binding to SearchResults.View with no success -->
</Grid>
View Model
public class MyViewModel
{
private ObservableCollection<MyItemViewModel> _allItems = new ObservableCollection<MyItemViewModel>();
public CollectionViewSource SearchResults { get; private set; }
public MyViewModel()
{
SearchResults = new CollectionViewSource { Source = _allItems };
_allItems.CollectionChanged += (_, __) => SearchResults.View.Refresh();
LoadAllItemsAsync();
}
private async void LoadAllItemsAsync()
{
IList<MyItemModel> models = await LoadMyModels();
_allItems.AddRange(models.Select(model => new MyItemViewModel(model)));
}
}
As you can see, I haven't even attempted to write filtering code yet. Shouldn't the code above basically show a LongListSelector with all the items I have loaded? If I change the SearchResults type to ObservableCollection<MyItemViewModel> and have the getter return _allItems, my page shows the expected items. What am I doing wrong here?