Problem
You need to manage your handler with the modified index list2, since every time you update your list, list 2 has the selected index change, and since there is no selected index, it defaults to -1.
There are several ways to do this.
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.
I'm not sure how you want your application to look.
I would use two separate pages for this. And have xaml for your first look at the list, and then the second page is scanned and snapped to the selected index of your first page.
So, you select list1, and then it is easier to set it as a data source on a new page that displays list2, and then you can update your text box with data from the selected item. or further, create a third page if you want to show more detailed information about the word and its definition.
Thus, you will have no problems with the fact that your List2 does not have the selected index when changing the data source.
3.
Or, Take the binding declarations from a handler with a modified index and call them methodically when the index in List1 is selected. Therefore, when the selection of List1 is changed, List 2 is updated in other words, you need to update your data source. edit: and with this other way, you control the use of error handling to eliminate the outofrange exception as the data source is updated.
Therefore, perhaps add the following to a separate method.
private void MyTextMethod(){ defBlock.Text = arrayDef[ListView1.SelectedIndex][ListView2.SelectedIndex]; } private void ListView2_SelectionChanged(object sender, SelectionChangedEventArgs e) { try{ MyTextMethod)(); } catch(OutOfRangeException){ // do something. } }
from a handler of your choice with a modified index and call a separate method when it is inside the handler.
4.
Take the binding declaration for list2 from your selectedindex change handler for list1.
So, you may have a method that will update the list2 binding source and manage the selected handler with a modified index. Although this is the least useful suggestion.
Bottom line: you need to have some try and catch or call an operator that controls the exception out of range, since the second list will have different lengths, and the index in the letter. The list can be selected at 10, and then the letter X can only have a list of length 1, and there is always a selection problem returning a choice of -1.
(You really do not need to clear list2, it clears automatically as the data source changes (sorry, I did not))