UWP Grid Element Selection Style

I want to change the highlight style or color of the gridview element. when an item is selected, I want to show a thicker border or highlight color or any similar changes. what is the easiest way to achieve this

+1
source share
1 answer

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.

enter image description here

Please mark this answer if this is useful to you!

-1
source

Source: https://habr.com/ru/post/1244141/


All Articles