1

我是一个初学者,正在为 Windows Phone 7.1 创建一个应用程序,我正在尝试通过列表框中的单击事件导航到新页面,基本上我希望列表框中的每个项目都应该导航到不同的页面我已尝试以下代码

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;
    }

上面的代码运行良好,但问题是它将整个列表框带到同一页面,但我希望每个单独的项目导航到不同的页面

4

2 回答 2

4

将列表框中的每个项目添加到列表框时,您可以设置它们的 SelectedValue。

假设您通过代码将字符串列表分配给 ListBox 的 ItemsSource(或者它可能通过数据绑定发生)。您可以在页面的构造函数中添加您的项目。

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

在 SelectionChanged 事件中添加一个 switch 语句,该语句根据所选项目的 SelectedValue 导航到不同的页面,如下所示:

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;
}

如果将复杂对象绑定到 ListBox,则可以使用 ListBox 的 SelectedItem-Property,因为该属性包含已绑定到该 ListBoxItem 的对象,并根据复杂对象的属性之一区分情况,例如姓名或身份证件。

于 2013-01-04T12:18:55.473 回答
0

我让我的应用程序使用此代码

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;
        }
    }
于 2013-01-06T04:38:53.013 回答