Justice
Justice

Reputation: 442

how to select an item in listbox in windows phone 7.1

i'm a beginner and creating an app for windows phone 7.1 and i'm trying to navigate to a new page through the click event in the the list box, basically i want that every item in the list box should navigate to different page i have tried the following code

private void FirstListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
     {
        // If selected index is -1 (no selection) do nothing
        if (FirstListBox.SelectedIndex == -1)
            return;

        // Navigate to the new page
        NavigationService.Navigate(new Uri("/FirstItemPage1.xaml?selectedItem=" + FirstListBox.SelectedIndex, UriKind.Relative));

        // Reset selected index to -1 (no selection)
        FirstListBox.SelectedIndex = -1;
    }

the above code works well but the problem is it takes the whole list box to the same page but i want that every individual item to navigate to a different page

Upvotes: 1

Views: 2534

Answers (2)

Justice
Justice

Reputation: 442

i got my app working with this code

private void SecondListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { { if (SecondListBox.SelectedIndex == -1) return;

           ItemViewModel itemViewModel = SecondListBox.SelectedItem as ItemViewModel ;

            switch (itemViewModel.LineOne)
            {
                case "Violet":
                    NavigationService.Navigate(new Uri("/SecondListPage1.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
                    break;
                case "Indigo":
                    NavigationService.Navigate(new Uri("/SecondListPage2.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
                    break;
                case "Blue":
                    NavigationService.Navigate(new Uri("/SecondListPage3.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
                    break;
                case "Green":
                    NavigationService.Navigate(new Uri("/SecondListPage4.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
                    break;
                case "Yellow":
                    NavigationService.Navigate(new Uri("/SecondListPage5.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
                    break;
                case "Orange":
                    NavigationService.Navigate(new Uri("/SecondListPage6.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
                    break;
                case "Red":
                    NavigationService.Navigate(new Uri("/SecondListPage7.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
                    break;
                case "Purple":
                    NavigationService.Navigate(new Uri("/SecondListPage8.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
                    break;
                default:
                    MessageBox.Show("Please Select From the list!");
                    break;




            }


            SecondListBox.SelectedIndex = -1;
        }
    }

Upvotes: 0

derSteve
derSteve

Reputation: 751

You can set the SelectedValue for each item in the listBox when adding them to the ListBox.

Let's say you assign a list of strings to the ItemsSource of the ListBox through code (or it may happend through data binding). You may add your items in the constructor of your page.

var items = new List<string>(){ "Home", "Details" };
FirstListBox.ItemsSource = items;

In the SelectionChanged event add a switch statement that navigates to different pages based on the SelectedValue of the selected item like so:

private void FirstListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
 {
    // If selected index is -1 (no selection) do nothing
    if (FirstListBox.SelectedIndex == -1)
        return;

    switch (FirstListBox.SelectedValue)
        {
            case "Home":// Navigate to the page
    NavigationService.Navigate(new Uri("/Home.xaml", UriKind.Relative)); break;
            case "Details":// Navigate to the page
    NavigationService.Navigate(new Uri("/Details.xaml", UriKind.Relative)); break;default:break;
        }


    // Reset selected index to -1 (no selection)
    FirstListBox.SelectedIndex = -1;
}

In case you Bind complex objects to the ListBox, you can use the SelectedItem-Property of the ListBox as that property contains the object that has been bound to that ListBoxItem and distinguish between the cases based on one of the properties of the complex object, like a name or id.

Upvotes: 4

Related Questions