0

以前はこのコードが機能していましたが、何か他のものを修正しようとしていたため、どういうわけか削除しました。

ビューモデルとしてバインドされたリストがあり、リストには3行あります。

リストをクリックして、別のページの 3 行目の値を取得し、その値を使用できるようにしたいと考えています。

リストを選択するためのコードは次のとおりです

private void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

        // If selected index is -1 (no selection) do nothing
        if (List.SelectedIndex == -1)
            return;

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

        string urlWIthData = string.Format("/Route.xaml?name={0}", " ");
        this.NavigationService.Navigate(new Uri(urlWIthData, UriKind.Relative));
        // Reset selected index to -1 (no selection)
        List.SelectedIndex = -1;
}

次に、ルートページで、値を取得して値の行 3 を使用できるようにしたい....

どうすればいいですか?

編集:

リストのこの一部:

this.Items.Add(new ItemViewModel() { LineOne = "Images/1.png", LineTwo = "Whitehawk - County   
Hospital - City Centre - Hove - Portslade - Mile Oak", LineThree = "1 Whitehawk - Mile Oak", 
LineFour = "1 Mile Oak - Whitehawk", LineFive = "1149" });

I リストを 1 ページに表示します。

 <ListBox Margin="6,6,7,6" ItemsSource="{Binding Items}" Name="List" OpacityMask="#FFD38648" FontSize="26" SelectionChanged="List_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,17" DataContext="{Binding}">
                        <Image Name="Images" Source="{Binding LineOne}">
                        </Image>
                        <TextBlock Text="{Binding LineTwo}" Style="{StaticResource PhoneTextSubtleStyle}" Name="textblock3"></TextBlock>
                                             </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
4

1 に答える 1

0

リストの SelectedItem を Item 型にキャストして、そこから値を取得できます。

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

       // Here's the codes -----------------------

       var item = Items.SelectedItem as Item;
       if(item == null) //cast didn't work
            return;

       var lineThree = item.LIneThree;

       // end of the codes -----------------------

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

        string urlWIthData = string.Format("/Route.xaml?name={0}", " ");
        this.NavigationService.Navigate(new Uri(urlWIthData, UriKind.Relative));
        // Reset selected index to -1 (no selection)
        List.SelectedIndex = -1;
}
于 2012-05-18T19:05:18.497 に答える