0

When a user clicks the item or items in the main page listview I need to take the data and pass it to a second array which will go to a checkout on the next page.I cant figure out how to pass data from 1 listview to another listview between pages.


    public struct DATA
    {
        public DATA(string distinguisher, double price, string description)
        {
            Distinguisher = distinguisher;
            Price = price;
            Description = description;
        }

        public string Distinguisher { get; }
        public double Price { get; }
        public string Description { get; }
    }

    public MainPage()
    {
        InitializeComponent();
    }

    public void btnBreakfast_Click(object sender)
    {
        //20 Elements for the Array
        DATA[] myData = new DATA[20]
        {
            new DATA("Breakfast", 4.00 ,  "Gourment Pancakes"),
            new DATA("Breakfast", 6.00 ,  "Eggs & Toast"),
            new DATA("Breakfast", 7.50 ,  "Oatmeal with OJ"),
            new DATA("Breakfast", 10.75 ,  "Fresh Waffles"),
            new DATA("Breakfast", 11.00 ,  "Bacon Egg & Cheese"),
            new DATA("Breakfast", 4.00 ,  "Bagel & Cream Cheese"),
            new DATA("Breakfast", 4.00 ,  "Butter Potatoes with Toast"),
            new DATA("Lunch", 9.50 ,  "Tuna Fish"),
            new DATA("Lunch", 8.00 ,  "Ham & Cheese"),
            new DATA("Lunch", 14.00 ,  "Buffalo Chicken Wrap"),
            new DATA("Lunch", 13.00 ,  "Cheeseburger with Fries"),
            new DATA("Lunch", 6.00 ,  " Jumbo Cheese Pizza"),
            new DATA("Lunch", 9.00,   "Hotdog with Fries"),
            new DATA("Lunch", 9.00,   "Philly Cheese Stake"),
            new DATA("Dinner", 22.00,   "Salmon with Two Sides"),
            new DATA("Dinner", 24.00,   "Steak with Two Sides"),
            new DATA("Dinner", 17.00,   "Chicken Parm Dinner"),
            new DATA("Dinner", 25.00,   "Extra Large Lasagna"),
            new DATA("Dinner", 15.00,   "Stuffed Shells"),
            new DATA("Dinner", 16.00,   "Penne Ala Vodka"),
        };

        for (int i = 0; i < myData.Length; i++)
        {
            if (myData[i].Distinguisher == "Breakfast")
            {
                HomePageListBox.Items.Add(myData[i].Description);
            }
        }
    }
Michael Puckett II
  • 6,586
  • 5
  • 26
  • 46
Mike M
  • 35
  • 1
  • Welcome to StackoverFlow. Please [take the tour](https://stackoverflow.com/tour), read about [how to ask good questions](https://stackoverflow.com/help/how-to-ask) and learn about [how to create a Minimal, Complete and Verifiable Example](https://stackoverflow.com/help/mcve). – Gaurang Dave Apr 06 '18 at 03:32

1 Answers1

1

I am not too clear what the real effect you want, but the principle should be same. Here is a complete example.

In the MainPage.xaml, there is a ListView,

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Button Content="Select" Click="btnBreakfast_Click"/>
    <ListView Grid.Row="1" Name="MainPageListView" ItemsSource="{Binding myData}" 
              SelectionMode="Multiple" SelectionChanged="MainPageListView_SelectionChanged">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Distinguisher}" Margin="5"/>
                    <TextBlock Text="{Binding Price}" Margin="5"/>
                    <TextBlock Text="{Binding description}" Margin="5"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

The MainPage.xaml.cs code behind with data model,

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        myData = new DATA[20]{
        new DATA("Breakfast", 4.00 ,  "Gourment Pancakes"),
        new DATA("Breakfast", 6.00 ,  "Eggs & Toast"),
        new DATA("Breakfast", 7.50 ,  "Oatmeal with OJ"),
        new DATA("Breakfast", 10.75 ,  "Fresh Waffles"),
        new DATA("Breakfast", 11.00 ,  "Bacon Egg & Cheese"),
        new DATA("Breakfast", 4.00 ,  "Bagel & Cream Cheese"),
        new DATA("Breakfast", 4.00 ,  "Butter Potatoes with Toast"),

        new DATA("Lunch", 9.50 ,  "Tuna Fish"),
        new DATA("Lunch", 8.00 ,  "Ham & Cheese"),
        new DATA("Lunch", 14.00 ,  "Buffalo Chicken Wrap"),
        new DATA("Lunch", 13.00 ,  "Cheeseburger with Fries"),
        new DATA("Lunch", 6.00 ,  " Jumbo Cheese Pizza"),
        new DATA("Lunch", 9.00,   "Hotdog with Fries"),
        new DATA("Lunch", 9.00,   "Philly Cheese Stake"),

        new DATA("Dinner", 22.00,   "Salmon with Two Sides"),
        new DATA("Dinner", 24.00,   "Steak with Two Sides"),
        new DATA("Dinner", 17.00,   "Chicken Parm Dinner"),
        new DATA("Dinner", 25.00,   "Extra Large Lasagna"),
        new DATA("Dinner", 15.00,   "Stuffed Shells"),
        new DATA("Dinner", 16.00,   "Penne Ala Vodka"),  };
        this.DataContext = this;
        passData = new List<DATA>();
    }

    public DATA[] myData { get; set; }


    public List<DATA> passData { get; set; }

    public void btnBreakfast_Click(object sender, RoutedEventArgs e)
    {
        //You can have a filter here to filter the data you want to pass to the new page.
        for (int i = 0; i < passData.Count; i++)
        {
            if (myData[i].Distinguisher == "Breakfast")
            {
                // HomePageListBox.Items.Add(myData[i].description);
            }
        }

        Frame.Navigate(typeof(NewPage), passData);

    }

    private void MainPageListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ListView list = sender as ListView;
        DATA selected = (DATA)list.SelectedItem;
        passData.Add(selected);
    }
}

public struct DATA
{
    public double Price { get; set; }
    public string description { get; set; }

    public string Distinguisher { get; set; }

    public DATA(string Distinguisher, double Price, string description)
    {
        this.Distinguisher = Distinguisher;
        this.Price = Price;
        this.description = description;

    }
}

After you select items, then click the button, the btnBreakfast_Click event triggers to navigate to the NewPage with the items data, then you can get the data in the NewPage's OnNavigatedTo method and show the data in your NewPage,

public NewPage()
{
    this.InitializeComponent();
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    //You can get data here, then you can display they in the xaml
   var data= e.Parameter as List<DATA>;
}
Breeze Liu - MSFT
  • 3,734
  • 1
  • 10
  • 13