I believe that the issue is with this line:
Pets selectedAnimal = lstCats.SelectedItem as Pets;
The problem, here, is that your ListBox
control has items that have Animal
s as their SelectedItem
s. What you will want to do is instead bind the ListBox
with pets, instead of items:
private void DisplayCats()
{
foreach (Pets temp in thisApp.pets)
{
if (temp.Category.Contains("Cat"))
{
lstCats.Items.Add(temp);
}
}
}
Update
Assuming you want to bind with Animal
objects, you can do the following:
private void DisplayCats()
{
foreach (Pets temp in thisApp.pets)
{
if (temp.Category.Contains("Cat"))
{
//note that I added the ID property --v
Animal animal = new Animal() { ID = temp.ID, Details = temp.Name + "\n" + temp.Category + " / " + temp.Subcategory + "\n€" + temp.Price.ToString(), ImageURI = temp.Image };
lstCats.Items.Add(animal);
}
}
}
Then your event handler should look like:
private void lstCats_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Animal selectedAnimal = lstCats.SelectedItem as Animal;
NavigationService.Navigate(new Uri("/ViewPet.xaml?msg=" + selectedAnimal.ID, UriKind.Relative));
}