0

我之前有这段代码工作过,但在我试图修复其他问题时以某种方式将其删除。

所以我有一个绑定为视图模型的列表,列表上有三行。

我希望能够单击列表,获取另一页上第三行的值并使用该值。

这是列表选择的代码

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

然后,在路线页面上,我希望能够获得价值并使用价值线三......

我该怎么做呢?

编辑:

所以这是列表的一部分:

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

我在一页上显示列表:

 <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 回答