用于wp8的longlistselector工具

时间:2013-04-15 13:46:19

标签: c# windows-phone-8

我目前正在使用Longlistselector处理Windows Phone 8。我正在列出我的产品列表,我需要获取所选项目索引,使用我需要导航到相应页面的索引。如何从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>

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

发件人是longlistselector并且选择它是特殊属性,例如: var t =(sender as LongListSelector).SelectedItem as Sample;