This question already has an answer here:
-
uwp win10 Listview SelectedItem Style
2 answers
I want to change the selection style or color of the gridview item. when an item is selected, I want to show a thicker border or a highlight color or any type of change like that. what is the simplest way to achieve this
Please check this example:
I have my page like this:
<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 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 I use ContentTemplateRoot with this property I have access to the principal container of my ItemTemplate.
Please mark this answer If it's useful for you!