无法在Xaml中将SelectedIndex =“0”设置为ListView(Windows应用商店应用)

问题描述:

我有2个ListViews和一个TextBlock。第一个ListView1包含按字母顺序排列的字母。第二个ListView2包含以选定字母开头的单词(在ListView1中)。当我从ListView1中选择一个字母,然后单击一个在ListView2中加载的单词时,我想要在TextBlock中获得该单词的定义。无法在Xaml中将SelectedIndex =“0”设置为ListView(Windows应用商店应用)

这是我的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];  
    } 

任何想法是错误我在做什么?

+0

这是第二次你点击列表1还是同一个字母? –

+0

这是我第二次点击不同的字母。给我的确切的错误是:索引超出了数组的范围。 – yalematta

+0

请使用您正在使用的Windows版本和特定应用类型对此进行标记。谢谢。 –

private void ListView2_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 

    if(ListView2.SelectedIndex >= 0){ 
     defBlock.Text = arrayDef[ListView1.SelectedIndex][ListView2.SelectedIndex];  
    } 
    else 
    { 
     defBlock.Text = arrayDef[ListView1.SelectedIndex][0];//set default selected word.. 
    } 
} 
+0

很好的答案,谢谢你。 –

问题
你需要管理你的列表2选择的指数变化的处理程序,因为每一次你更新你的列表中的一个存在于列表2选择的指数变化,因为没有选择指数则默认为-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没有选定索引的问题。


或者, 就拿绑定声明了该指数的变化处理程序,有条不紊地给他们打电话时的指数是在列表1是selected.So当列表1的选择发生变化时,清单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的绑定源并管理选定的索引改变处理程序。虽然这是最没有用的建议。

底线:您需要有一些尝试和捕捉,或抛出声明管理超出范围的异常,因为第二个列表将具有不同的长度,并且字母As列表中的索引可以选择10,然后字母X可能只有一个长度为1的列表,并且始终存在selectionchange返回选择-1的问题。

(你实际上并不需要清除列表2,作为数据源改变时它会自动清零(对不起,我没有说清楚))

+0

我创建了一个方法: 'public void FillWords() { ListView2.Items.Clear(); ListView2.ItemsSource = arrayW [ListView1.SelectedIndex]; }' 我从ListView1的SelectionChanged事件处理程序调用它。但是我在'ListView2.Items.Clear();'上得到了一个“灾难性失败”错误;' – yalematta

+0

因为我有SelectionMode = SingleMode,所以我不能使用'ListView2.Items.Clear();'我使用了'ListView2。 SelectedItem = null;'但我仍然得到第一个错误'OutOfRange'错误'ListView2.SelectedIndex = -1' – yalematta

+0

对不起,我想我的第一个答案不清楚。这是我的错。我希望这会为你清除它。 :) –