I have a list with a simple list of items. On my xaml page, I have the following
<ListBox Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding firstName}"/>
<TextBlock Text="{Binding lastName}"/>
<Button BorderThickness="0" Click="buttonPerson_Click">
<Image Source="delete-icon.png"/>
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In my code, I am trying to grab selectedIndex so that I can remove an item from the collection bound to my list.
private void buttonPerson_Click(object sender, RoutedEventArgs e)
{
if (listBox1.SelectedIndex == -1)
return;
myPersonList.removeAt(listBox1.SelectedIndex);
}
However, no matter which line I click on the delete button, selectedIndex is always -1
What am I missing?
early!
source
share