问题描述
我想更改gridview项目的选择样式或颜色。当选择一个项目时,我想显示较粗的边框或高光颜色或任何类型的更改。什么是最简单的方法来实现这一点
请检查这个例子:
< 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>
<网格>
< ListView x:Name =MyList>
< ListView.ItemTemplate>
< DataTemplate>
< Grid x:Name =MyGrid>
< TextBlock Text =Test1/>
< / Grid>
< / DataTemplate>
< /ListView.ItemTemplate>
< / ListView>
< / Grid>
我填写清单:
保护覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
MyList.SelectionChanged + = MyList_SelectionChanged;
var list = new List< string>();
list.Add(1);
list.Add(2);
MyList.ItemsSource = list;
}
最后,我得到选定的项目,然后更改背景
private void MyList_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
var item = MyList.ContainerFromItem(e .AddedItems.FirstOrDefault());
var selectedItem = item作为ListViewItem;
if(selectedItem!= null)
{
var grid = selectedItem.ContentTemplateRoot as Grid;
grid.Background =新的SolidColorBrush(Colors.Yellow);
}
}
如果您看到我对此属性使用ContentTemplateRoot,则可以访问我的ItemTemplate的主容器。
p>
请标记此答案如果它对您有用!
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!
这篇关于UWP gridview项目选择样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!