I have a listbox with several items . And the selection changed event has been mapped to a function. But everytime i select a listbox item, event is getting triggered twice. As result i am getting the selected index as -1 on the second round .
anyone knows the reason for this behaviour?
XAML code
<ListBox ItemTemplate="{StaticResource productTemplate}" Name="lstItems" SelectionChanged="ShowProductDetails">
C# code
private void ShowProductDetails(object sender, SelectionChangedEventArgs e)
{
Item item = lstItems.SelectedItem as Item;
int index = lstItems.SelectedIndex;
}
A selection changed event happens everytime the selection is changed (either from user input or not)
When you reload the listbox the selection changes (most likely to -1)
this code below checks to ensure that a user has selected something (an item with a valid index i.e. Not -1) then runs the code.