传递绑定列表的值

时间:2012-05-18 18:35:12

标签: windows-phone-7

我以前使用过这段代码,但是当我尝试修复其他内容时,它已经以某种方式删除了它。

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

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

以下是选择列表的代码

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>

1 个答案:

答案 0 :(得分: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;
}