3

我目前正在使用 windows phone 8 ,使用Longlistselector. 我正在列出我的产品列表,我需要获取选定的项目索引,使用该索引我需要导航到相应的页面。如何从中获取选定的索引longlistselector?我得到了selectedItem,但我不知道如何使用selecteditem来获取它的索引?提前致谢

我的长名单选择器代码是

<phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="trainlistitemtemplate">
            <StackPanel Orientation="Horizontal">
                <Image Height="170" Width="170" Source="{Binding Imgcity}" Margin="0,0,9,0"></Image>
                <StackPanel VerticalAlignment="Top">
                    <TextBlock FontWeight="Bold" Text="{Binding Cityname}" />
                    <TextBlock Text="{Binding Citycode}"/>
                    <Button Content="BOOK" BorderBrush="{x:Null}" Background="{StaticResource PhoneAccentBrush}" Click="Button_Click_1"></Button>
                </StackPanel>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Name="grpheadtemplate">
            <Border Background="Transparent" Padding="5">
                <Border Background="{StaticResource PhoneAccentBrush}" BorderBrush="{StaticResource PhoneAccentBrush}" BorderThickness="2" Width="62" 
                        Height="62" Margin="0,0,18,0" HorizontalAlignment="Left">
                    <TextBlock Text="{Binding Key}" Foreground="{StaticResource PhoneForegroundBrush}" FontSize="48" Padding="6" 
                                FontFamily="{StaticResource PhoneFontFamilySemiLight}" HorizontalAlignment="Left" VerticalAlignment="Center">
                    </TextBlock>
                </Border>
            </Border>
        </DataTemplate>

        <phone:JumpListItemBackgroundConverter x:Key="BackgroundConverter"/>
        <phone:JumpListItemForegroundConverter x:Key="ForegroundConverter"/>
        <Style x:Key="trainlist" TargetType="phone:LongListSelector">
            <Setter Property="GridCellSize"  Value="113,113"/>
            <Setter Property="LayoutMode" Value="Grid" />
            <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Border Background="{Binding Converter={StaticResource BackgroundConverter}}" Width="113" Height="113" Margin="6" >
                            <TextBlock Text="{Binding Key}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" FontSize="48" Padding="6" 
               Foreground="{Binding Converter={StaticResource ForegroundConverter}}" VerticalAlignment="Center"/>
                        </Border>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </phone:PhoneApplicationPage.Resources>
    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Select Destination" Style="{StaticResource PhoneTextNormalStyle}" FontSize="40" FontFamily="Batang" FontWeight="Bold"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->

        <Grid Grid.Row="1">
            <phone:LongListSelector HorizontalAlignment="Left" Height="703" Margin="10,10,0,-36" VerticalAlignment="Top" Width="458" x:Name="lls1"
                                    Background="Transparent" HideEmptyGroups="True" LayoutMode="List" IsGroupingEnabled="True"
                                    ItemTemplate="{StaticResource trainlistitemtemplate}" GroupHeaderTemplate="{StaticResource grpheadtemplate}"
                                    JumpListStyle="{StaticResource trainlist}" MouseLeftButtonDown="lls1_MouseLeftButtonDown" SelectionChanged="lls1_SelectionChanged"/>
        </Grid>
    </Grid>
4

2 回答 2

1

您真正想要的不是获取索引,而是获取DataContext用户点击的项目的。我想,您正在使用数据绑定来填充 LongListSelector。然后,您需要监听Tap列表项上的事件,并且在该事件的处理程序中,您需要检索DataContext属性,将其转换为所需的类型,并使用该值来决定转到哪个页面。这已在 StackOverflow 上多次讨论过,例如,请参阅此答案。

于 2013-04-16T09:56:50.203 回答
0

sender 是 longlistselector 并且选定的项目是特定属性,例如: var t = (sender as LongListSelector).SelectedItem as Sample;

于 2013-05-10T09:14:18.203 回答