Please check this example:
I have my page:
<Page x:Class="App3.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="using:App3" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid> <ListView x:Name="MyList"> <ListView.ItemTemplate> <DataTemplate> <Grid x:Name="MyGrid"> <TextBlock Text="Test1" /> </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView> </Grid>
I fill out my list:
protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); MyList.SelectionChanged += MyList_SelectionChanged; var list = new List<string>(); list.Add("1"); list.Add("2"); MyList.ItemsSource = list; }
Finally, I get the selected item and I change the background
private void MyList_SelectionChanged(object sender, SelectionChangedEventArgs e) { var item= MyList.ContainerFromItem(e.AddedItems.FirstOrDefault()); var selectedItem = item as ListViewItem; if (selectedItem != null) { var grid = selectedItem.ContentTemplateRoot as Grid; grid.Background = new SolidColorBrush(Colors.Yellow); } }
If you see that I am using ContentTemplateRoot with this property, I have access to the main container of my ItemTemplate.

Please mark this answer if this is useful to you!
source share