UWP gridview item selection style [duplicate]

删除回忆录丶 提交于 2019-12-24 13:35:13

问题


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


回答1:


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!



来源:https://stackoverflow.com/questions/35703823/uwp-gridview-item-selection-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!