1
votes

I am creating an app where I have obtained data from a DB Via Web service and displaying it within a listbox within my mainpage.xaml (data about different events being held e.g. EventTitle, Date etc.) At the moment I have set it up so that when an item within the list box is selected the application navigates to an "EventDetail" page which portrays only the selected data from the list.

What I need help with is taking this user selected data and taking it to the navigated page to be used. I am struggling to figure out in the simplest way; how to transfer a listbox item which has been selected by the user to the defined page to display it in a more clear way and to use this data.

Preferably I would like to display each textblock/cell (EventTitle, Date etc) in separate text blocks within the navigated page so that they can be laid out appropriately but a list box with only the selected fields data would do the job.

Here is the relevant Xaml code:

   <ListBox  Height="496" HorizontalAlignment="Left" Margin="-4,155,0,0" Name="FirstListBox2" VerticalAlignment="Top" Width="460" SelectionChanged="FirstListBox2_SelectionChanged">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="132">

                            <StackPanel Width="370">
                                <TextBlock Text="{Binding EventID}" Foreground="#FFC8AB14" FontSize="24" />
                                <TextBlock Text="{Binding EventList}" TextWrapping="Wrap" FontSize="36" />
                                <TextBlock Text="{Binding Date}" TextWrapping="Wrap" FontSize="24" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

Here is the relevant C# coding:

     private void FirstListBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        EventServiceReference1.Event myEvent = (EventServiceReference1.Event)FirstListBox2.SelectedItem;
        NavigationService.Navigate(new Uri("/EventPageTemp.xaml", UriKind.Relative));
    }

Please if you could help me with this problem I would very much appreciate.

2

2 Answers

1
votes

Use a query-string parameter to send the selected value:

EventServiceReference1.Event myEvent = (EventServiceReference1.Event)FirstListBox2.SelectedItem;
int eventId = myEvent.EventID;
string url = string.Format("/EventPageTemp.xaml?eventId={0}", eventId);
NavigationService.Navigate(new Uri(url, UriKind.Relative));

You can pick up the event parameter on the target page using NavigationContext.QueryString. Then just set the data context of your target page as needed:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    int eventId;
    string eventIdStr;
    if (NavigationContext.QueryString.TryGetValue("eventId", out eventIdStr) && int.TryParse(eventIdStr, out eventId))
    {
        // load event data, and set data context
    }
}

Edit

I guess you might want to save the event data temporarily, rather than re-loading it from the service on the target page. If so, you could use isolated storage to save the event data. So before navigating, add the data using an appropriate key:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["event"] = myEvent;
settings["eventId"] = eventId;

And then pick it up in the same way:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (settings.ContainsKey("event") && (int)settings["eventId"] == eventId)
{
    var myEvent = (EventServiceReference1.Event)settings["event"];
}

Edit #2

As AndreiC points out, writing to isolated storage takes up space on the device, so you should remove any items that are no longer needed.

1
votes

A simple approach would be to add a public static property of type Event to your App class (name it SelectedEvent) so when the user selects an event from the ListBox you would set the:

App.SelectedEvent = myevent;

Then in the EventPageTemp.xaml.cs you would just set the DataContext of the page to your App.SelectedEvent:

DataContext = App.SelectedEvent;

In the xaml page of the EventPageTemp you would just bind the interface to the properties of your Event object.