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

ListBox にアイテムを追加するときに、listBox 内の各アイテムの SelectedValue を設定できます。

コードを使用して文字列のリストを ListBox の ItemsSource に割り当てるとします (または、データ バインディングによって発生する可能性があります)。ページのコンストラクターに項目を追加できます。

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

次のように、SelectionChanged イベントで、選択した項目の SelectedValue に基づいて別のページに移動する switch ステートメントを追加します。

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 にバインドされたオブジェクトが含まれているため、複雑なオブジェクトのプロパティの 1 つに基づいてケースを区別できます。名前またはID。

于 2013-01-04T12:18:55.473 に答える
0

このコードでアプリが動作するようになりました

private void SecondListBox_SelectionChanged(オブジェクト送信者, 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 に答える