无法设置SelectedIndex =" 0"在Xaml到ListView(Windows应用商店应用)

时间:2015-11-05 13:49:46

标签: c# xaml listview windows-store-apps selectedindex

我有2个ListViews和一个TextBlock。第一个ListView1包括按字母顺序排列的字母。第二个ListView2包含以所选字母开头的单词(在ListView1中)。当我从ListView1中选择一个字母,然后单击ListView2中加载的单词时,我想在TextBlock中获得该单词的定义。

这是我的Xaml:

<ListView
               Width="510"
               x:Name="ListView1"
               ItemsSource="{Binding}" 
               Background="White"
               Foreground="Black"
               TabIndex="1"
               Margin="-7,8,0,0"
               IsSwipeEnabled="False"

               SelectionChanged="ItemListView_SelectionChanged"
               Grid.Row="1"
               HorizontalAlignment="Left">
               <ListView.ItemTemplate>
                   <DataTemplate>
                       <StackPanel>
                           <TextBlock Grid.Row="0"
                           Text="{Binding glossary_letter}"
            Margin="10,0,0,0" 
            TextWrapping="Wrap"
                           Foreground="Black"
            FontSize="24"
            FontWeight="SemiBold"
            VerticalAlignment="Center"/>
                       </StackPanel>
                   </DataTemplate>
               </ListView.ItemTemplate>
           </ListView>
           <ListView  Width="361"
               x:Name="ListView2"
               Background="White"
               Foreground="Black"
               Margin="425,8,230,0"
               Grid.Row="1"
               HorizontalAlignment="Center"
               ItemsSource="{Binding}"
               SelectionChanged="itemListView2_SelectionChanged">
               <ListView.ItemTemplate>
                   <DataTemplate>
                       <StackPanel>
                           <TextBlock Grid.Row="1"
            TextWrapping="Wrap"
                           Foreground="Black"
                           Text="{Binding}"       
            FontSize="24"
            FontWeight="SemiBold"
            VerticalAlignment="Center"/>
                       </StackPanel>
                   </DataTemplate>
               </ListView.ItemTemplate>
           </ListView>
           <StackPanel HorizontalAlignment="Right"
                       Background="White"
                       Width="580"
                       Margin="0,10,0,0" Grid.Row="1" Grid.ColumnSpan="2">
               <TextBlock x:Name="defBlock" Foreground="Black" Text="{Binding glossary_definition}"></TextBlock>
           </StackPanel>

如果我第一次点击一个字母(ListView1)然后单击一个单词(ListView2),它会显示我的定义。但是,当我第二次点击一封信时,它会给OutOfRange

ListView2.SelectedIndex = -1错误

这是我的C#代码:

private void ListView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
       {           
           ListView2.ItemsSource = arrayW[ListView1.SelectedIndex];          
       }
   private void ListView2_SelectionChanged(object sender, SelectionChangedEventArgs e)
   {
     defBlock.Text = arrayDef[ListView1.SelectedIndex][ListView2.SelectedIndex];       
   }

知道我在做什么错误吗?

2 个答案:

答案 0 :(得分:2)

count

答案 1 :(得分:0)

问题
您需要管理list2选择的索引更改处理程序,因为每次更新列表时,列表2上都会有一个选定的索引更改,并且没有选定的索引,它默认为-1。

有很多方法可以做到这一点 的 1

private void ListView2_SelectionChanged(object sender, SelectionChangedEventArgs e)
   {
     if(ListView2.SelectedIndex == -1)
     // do something or
     // eg.
     return;
     // or
     throw new IndexOutOfRangeException("Message");
     //or 
     throw new Exception(); // catch all              
   }

<强> 2

我不确定您希望自己的应用看起来像什么。

我正在使用两个单独的页面。并为您的第一个列表视图设置xaml,然后查看第二页并将其绑定到第一页的选定索引。

所以list1,你选择,然后更容易在显示list2的新页面中设置为数据源,然后你可以用所选项目的详细信息更新文本框。或者,如果您想要显示该词及其定义的更多详细信息,请创建第三页。

这样,当数据源发生变化时,List2没有选择索引就没有问题。

第3
要么, 从索引更改处理程序中取出绑定声明,并在选择List1中的索引时有条理地调用它们。因此,当更改List1的选择时,更新列表2,换句话说,您需要更新数据源。 编辑:通过这种方式,您可以控制错误处理的使用,以避免超范围异常,因为数据源已更新。

所以可能将以下内容放在一个单独的方法中。

private void MyTextMethod(){

    defBlock.Text = arrayDef[ListView1.SelectedIndex][ListView2.SelectedIndex];  
}

private void ListView2_SelectionChanged(object sender, SelectionChangedEventArgs e)
   {
     try{

        MyTextMethod)();
     }
     catch(OutOfRangeException){ 
        // do something.
     }
}

从您选择的索引更改处理程序并从处理程序内部调用单独的方法。

<强> 4

从list1的selectedindex更改处理程序中获取list2的绑定声明。

所以你可以有一个方法来更新list2的绑定源并管理所选索引改变的处理程序。虽然这是最不实用的建议。

底线:你需要有一些try和catch,或者抛出语句来管理outofrange异常,因为第二个列表将有不同的长度,字母As列表上的索引可以选择10,然后是字母X可能只有一个长度为1的列表,并且总是存在选择更改的问题,返回-1的选择。

(您实际上并不需要清除list2,它会在数据源发生变化时自动清除(抱歉,我没有说清楚))