我有一个GridView
with items 和一个数组,其中包含有关我的GridView
. 我想获取单击项目的索引,我尝试使用我之前使用的相同代码ListView
:
private void GridView1_ItemClick(object sender, ItemClickEventArgs e)
{
int test = GridView1.Items.IndexOf(e.ClickedItem);
}
当我调试我的代码时, inttest
将始终具有 value -1
。我不知道为什么这不起作用,我希望你们中的一个人这样做。
编辑:感谢所有回复!这里有一些额外的信息:我已经禁用了,SelectionMode
因为我只想捕获ClickedItem
. 我想知道索引,因为它链接到我的额外信息数组。
由于我SelectionMode
的设置为“无”,因此我无法捕获 SelectedItem/SelectedIndex。我尝试启用它们,但再次收到值“-1”。
编辑#2:ItemSource
自从我手动添加项目以来,我没有设置任何内容。这可能是问题吗?我ListView
使用 no 没有问题ItemSource
。
编辑#3:
C#代码:
TextBlock content_textblock = new TextBlock();
content_textblock.Width = 250;
content_textblock.Text = total;
content_textblock.TextWrapping = TextWrapping.Wrap;
content_textblock.Foreground = (SolidColorBrush)Application.Current.Resources["ListViewItemOverlayForegroundThemeBrush"];
content_textblock.Style = (Style)Application.Current.Resources["CaptionTextStyle"];
content_textblock.LineHeight = 30;
content_textblock.Padding = new Thickness(5, 0, 10, 15);
content_textblock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
content_textblock.Arrange(new Rect(0, 0, content_textblock.DesiredSize.Width, content_textblock.DesiredSize.Height));
StackPanel content_stackpanel = new StackPanel();
content_stackpanel.Margin = new Thickness(0, 250 - content_textblock.ActualHeight, 0, 0);
content_stackpanel.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
content_stackpanel.Height = content_textblock.Height;
content_stackpanel.Width = 250;
content_stackpanel.Background = (SolidColorBrush)Application.Current.Resources["ListViewItemOverlayBackgroundThemeBrush"];
content_stackpanel.Children.Add(content_textblock);
ImageBrush content_brush = new ImageBrush();
content_brush.ImageSource = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("http://ic.tweakimg.net/ext/i/thumbs_fpa_small/1357208162.jpeg")); //it is a placeholder, haha
GridView1.Items.Add(new GridViewItem { Background = content_brush, Content = content_stackpanel, Width = 250, Height = 250 });
XAML:
<GridView x:Name="GridView1" Margin="0,29,0,0" Grid.Row="2" SelectionMode="None" ItemClick="GridView1_ItemClick" IsItemClickEnabled="True"/>