Change gridViewItem’s background color after first and second click on it
我的意思是在第一次单击后将 gridViewItem 的背景颜色更改为蓝色,在第二次单击后更改为红色,然后是蓝色,然后是红色…
这是我的 c# 代码,但它在 “gvi.Background = new SolidColorBrush(Windows.UI.Colors.Blue);”:
上引发了异常 //’System.NullReferenceException//’
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
private void GridViewItem_Click (Object sender, ItemClickEventArgs e) { if(e!=null) { for (int numberOfClick= 1; numberOfClick <100; ++numberOfClick) if (numberOfClick % 2 == 0) gvi.Background = new SolidColorBrush(Windows.UI.Colors.Red); } } |
这是我的
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<Page x:Class="HNT_listView2.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:HNT_listView2" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:data="using:HNT_listView2.Models" mc:Ignorable="d"> <Grid Background="Salmon" Margin="0,0,10,0" > <GridView ItemsSource="{x:Bind MyContactList}" <GridView.ItemTemplate> <StackPanel HorizontalAlignment="Center" Orientation="Horizontal"> <Image Width="100" Height="120" Source="{x:Bind Photo}" HorizontalAlignment="Center" Stretch="UniformToFill"/> </GridView> </Grid> |
模型中的我的 Contact.cs:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class Contact { public string Name { get; set; } public string Photo { get; set; } public string Phone { get; set; } } public class ContactManager return contact1; |
任何帮助将不胜感激,谢谢!
为什么不使用布尔值作为计数器?
1
2 3 4 5 6 7 8 9 10 11 12 13 14 |
private byte GridViewSwitch = 0; private void GridViewItem_Click (Object sender, ItemClickEventArgs e) { if(e!=null) { var item = (GridViewItem)NameOf_ItemClick.ContainerFromItem(e); switch(GridViewSwitch) case 0 : item.Background = new SolidColorBrush(Windows.UI.Colors.Red); GridViewSwitch++; break ; case 1 : item.Background = new SolidColorBrush(Windows.UI.Colors.Blue); GridViewSwitch++; break ; case 2: item.Background = new SolidColorBrush(Windows.UI.Colors.Green); GridViewSwitch=0; break ; } |
如果您认为此答案正确且有帮助,请单击左侧的勾号进行批准!
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/269656.html