I am trying to create a foreach loop for a listview to detect which items are selected (multiselection is on) and then concatenate the item's text to a string to pass to a second page with the frame.navigate method.
I've googled and several solutions on here suggest using the listview.CheckedItems or .SelectedItems, some older ones referenced .IsSelected or .IsChecked. none of these seem to be valid in the Visual Studios version I'm using. I've used the updater to ensure I have the most recent version Visual Studios.
To be clear: I'm not asking for someone to rewrite my homework, but to find out what the working replacement for listview.checkeditems is, or if this simply no longer works what should I be using instead of a listview for the user to select items that I can detect.
Here are the instructions to my homework.
Create two arrays ( 20 elements). The first array will contain all of the items available including a Distinguisher between breakfast, lunch and dinner, the item description and the price. Populate this array though a loaded event method.
On the first page, provide three buttons to choose breakfast, lunch or dinner. When the user clicks a button, have the items for that meal description( no price) appear in a listview box on top portion of the page and also record them in the second array.
Add a checkout button to the page. When clicked go to a second page.
On the second page, display the items selected ( already stored in second array) including price in a list view box. Provide the total of cost of the items.
Everything up to the checkout button works. I've populated the array from a text file, the meal buttons load the appropriate foods into the listview and I know how to pass strings with Frame.Navigate but if I can't detect what Listview items are checked I can't pass them!
I have done a LOT of google searching to find a solution, we have no textbook for this class and the teachers answer is to re-watch some tutorial videos (half of which are for windows 8 phones and are no longer entirely correct).
Here is the snippet of code for my checkout button
private void CheckoutButton_Click(object sender, RoutedEventArgs e)
{
foreach (ListViewItem eachItem in MenuList.CheckedItems)
{
checkedMenuItems = checkedMenuItems + "/" + MenuList.Items.ToString();
}
}//end CheckoutButton_Click
The error returned for CheckedItems is
'ListView' does not contain a definition for 'CheckedItems' and no extension method 'CheckedItems' accepting a first argument of type 'ListView' could be found (are you missing a using directive or an assembly reference?)
The same error is returned for SelectedItems and no solutions are suggested by Visual Studios.
I am hoping that someone can help!
Thanks.
I'm adding the code for the whole MainPAge in case it is relevant. It references class (breakfast, lunch, dinner) in a separate .cs file.
public sealed partial class MainPage : Page
{
public static meal[] menuItems = new meal[20];
public static meal[] menuChoices = new meal[20];
public string checkedMenuItems;
public MainPage()
{
this.InitializeComponent();
Loaded += PopulateArray;
}
public async void PopulateArray(object sender, RoutedEventArgs e)
{
int counter = 0;
char switchCheck;
var file = await StorageFile.GetFileFromApplicationUriAsync(new
Uri("ms-appx:///MenuItems.txt"));
using (var inputStream = await file.OpenReadAsync())
using (var classicStream = inputStream.AsStreamForRead())
using (var streamReader = new StreamReader(classicStream))
{
while (streamReader.Peek() >= 0)
{
string itemIn = string.Format(streamReader.ReadLine());
string[] item = itemIn.Split('/');
switchCheck = Convert.ToChar(item[0]);
switch (switchCheck)
{
case 'b':
menuItems[counter] = new
breakfast(Convert.ToChar(item[0]), item[1], Convert.ToDecimal(item[2]));
// MenuList.Items.Add("works item# " + counter +
menuItems[counter].getMealType());
break;
case 'l':
menuItems[counter] = new
lunch(Convert.ToChar(item[0]), item[1], Convert.ToDecimal(item[2]));
break;
case 'd':
menuItems[counter] = new
dinner(Convert.ToChar(item[0]), item[1], Convert.ToDecimal(item[2]));
break;
}//end switch
counter++;
}//end while`
}
}//end populateArray method
private void BreakfastButton_Click(object sender, RoutedEventArgs e)
{
MenuList.Items.Clear();
for (int i = 0; i < menuItems.Length && menuItems[i] != null; i++)
{
if (menuItems[i].getMealType() == 'b')
MenuList.Items.Add(menuItems[i].getMealDesc());
}//end for loop
}//end BreakfastButton_Click
private void LunchButton_Click(object sender, RoutedEventArgs e)
{
MenuList.Items.Clear();
for (int i = 0; i < menuItems.Length && menuItems[i] != null; i++)
{
if (menuItems[i].getMealType() == 'l')
MenuList.Items.Add(menuItems[i].getMealDesc());
}//end for loop
}//end LunchButton_Click
private void DinnerButton_Click(object sender, RoutedEventArgs e)
{
MenuList.Items.Clear();
for (int i = 0; i < menuItems.Length && menuItems[i] != null; i++)
{
if (menuItems[i].getMealType() == 'd')
MenuList.Items.Add(menuItems[i].getMealDesc());
}//end for loop
}//end DinnerButton_Click
private void CheckoutButton_Click(object sender, RoutedEventArgs e)
{
foreach (ListViewItem eachItem in MenuList.CheckedItems)
{
checkedMenuItems = checkedMenuItems + "/" +
MenuList.Items.ToString();
}
}//end CheckoutButton_Click
}//end mainpage