Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set SelectedIndex="0" in Xaml to ListView (Windows Store App)

I have 2 ListViews and a TextBlock. The first ListView1 includes letters in Alphabetical order. And the second ListView2 includes the words that start with the selected letter (in ListView1). When I choose a letter from ListView1 and then click on a word loaded in ListView2, I want to get the definition of this word in a TextBlock.

This is my 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>

If I click the first time on a letter (ListView1) then on a word (ListView2) it shows me the definition. However the second time I click on a letter, it gives me an OutOfRange Error where the ListView2.SelectedIndex = -1

This is my C# code:

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];       
   }

Any idea what is the error I am doing?

like image 517
yalematta Avatar asked Oct 31 '22 16:10

yalematta


1 Answers

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.. 
    }
}
like image 194
Taleb Atoui Avatar answered Nov 13 '22 02:11

Taleb Atoui