Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get selected ListViewItem container from ListView in Windows Universal App

Tags:

c#

xaml

uwp

Is there a way to get the ListViewItem container when a ListViews selection has changed. I've tried using

listView.ItemContainerGenerator.ContainerFromItem(listView.SelectedItem);

from the ListViews selection changed event but this always returns null. Any ideas what I'm doing wrong?

like image 833
Real World Avatar asked Dec 17 '15 20:12

Real World


1 Answers

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.

like image 78
Bart Avatar answered Oct 21 '22 01:10

Bart