简体   繁体   中英

longlistselector tool for wp8

I'm currently working on a windows phone 8 , using Longlistselector . I am listing my product list, and I need to get the selected item index, using that index I need to navigate to corresponding page. How do I get the selected index from longlistselector ? I'm getting the selectedItem , but I don't know how to use the selecteditem to get its index? Thanks in advance

my longlist selector code is

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

What you really want is not to get index, but to get the DataContext of the item being tapped by user. I suppose, you're using data binding to populate the LongListSelector. Then you need to listen to Tap event on an list item, and in that event's handler you need to retrieve the DataContext property, cast it to your required type, and use that value to decide to which page to go. This been discussed on StackOverflow multiple times, see this answer for example.

sender是longlistselector,selected ites是特殊的属性,例如:var t =(sender as LongListSelector).SelectedItem as Sample;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM