Reputation: 1719
Is there a way to get the ListViewItem
container when a ListView
s selection has changed. I've tried using
listView.ItemContainerGenerator.ContainerFromItem(listView.SelectedItem);
from the ListView
s selection changed event but this always returns null. Any ideas what I'm doing wrong?
Upvotes: 9
Views: 3610
Reputation: 10015
XAML:
<ListView ItemsSource="{x:Bind RowItems}"
SelectionChanged="ListView_OnSelectionChanged"/>
Code-behind:
private void ListView_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = e.AddedItems?.FirstOrDefault();
// edit: also get container
var container = ((ListViewItem) (listView.ContainerFromItem(item)));
}
Note that listView.ItemContainerGenerator.ContainerFromItem
is obsolete since Windows 8.1.
Upvotes: 12