I have defined ItemClick event handler for a ListView. The event handler is called, but when I click the ListViewItem for the first time SelectedItem of the ListView is null and SelectedIndex is -1. But I want to retrieve SelectedItem also on the first click when nothing is selected in the ListView. Is there another solution? Thanks.
2
votes
3 Answers
4
votes
2
votes
2
votes
More appropriately for the click event, get the clicked item index. For example,
private void Column_ItemClick(object sender, ItemClickEventArgs e)
{
ListView listView = (ListView)sender;
Tile clickedMenuItem = (Tile)e.ClickedItem; // Typecast the clicked menu item back to the content type of each list view item
// Key line is here:
int itemNumber = listView.Items.IndexOf(clickedMenuItem);
}
In the above on click event of the listView.ItemClick += Column_ItemClick event, the itemNumber integer is the index of the clicked list view item.
This solution avoids the -1 index response for a selected item, because the on click event that selects an item happens only after the second click of the menu.