none
无法获取ListViewItem在第几列 RRS feed

  • 问题

  • 我绑定了一个ListView,然后添加了一个事件,但是获取的Index一直是-1.

    ListView是有数据的,cs中已经通过VisualTree拿到了ListView。

                <HubSection x:Name="HubS2" x:Uid="HubSection2" >
                    <DataTemplate>
                        <ListView x:Name="myListView"
                            AutomationProperties.AutomationId="ItemListView" AutomationProperties.Name="Items In Group"
                                 TabIndex="1"
                                 Grid.Row="1"
                                 ItemsSource="{Binding Items}"
                                 IsItemClickEnabled="True"
                                 SelectionMode="None"
                                 IsSwipeEnabled="false"
                                Margin="-10,0,-0.167,0.167" IsDoubleTapEnabled="False"
                                ItemClick="myListView_ItemClick"
                                  >
    
                         <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Grid x:Name="HubS2Grid" Tag="MyGridS2" Width="Auto" Height="Auto" VerticalAlignment="Top" HorizontalAlignment="Left">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                     
                                        </Grid.ColumnDefinitions>
                                        
                                        <!--布局需要根据屏幕分辨率来调整。-->
                                        <Border  Margin="0,45,0,0" Width="79" Height="79">
                                            <Image Source="{Binding ImgPathCollection}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
                                        </Border>
                                        <StackPanel Grid.Column="1" VerticalAlignment="Stretch" Margin="14.5,45,0,0">
                                            <TextBlock Text="{Binding menuItem}" Style="{ThemeResource ListViewItemTextBlockStyle}"/>
                                            <TextBlock Text="" Style="{ThemeResource ListViewItemContentTextBlockStyle}" Foreground="{ThemeResource PhoneMidBrush}"/>
                                            <TextBlock x:Name="desc" Text="{Binding submenuItem}" Style="{ThemeResource ListViewItemSubheaderTextBlockStyle}"/>
                                        </StackPanel>
                                    </Grid>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </DataTemplate>
                </HubSection>
            </Hub>
    private void myListView_ItemClick(object sender, ItemClickEventArgs e)
        {
            ListView myListView = FindFirstVisualChild<ListView>(HubS2,"myListView");
            Object o = myListView.SelectedItems;
            int index =myListView.SelectedIndex;//这里显示为-1
            if (o == null)
                return;
            ListViewItem item = o as ListViewItem;
    
                
        }


    2015年4月6日 10:37

答案

全部回复