我有一個GridView
項目和一個數組,其中包含有關我的GridView
中特定項目的詳細信息。 我想被點擊項目的指標,我試圖用我前面有ListView
使用相同的代碼:Windows 8 C#GridView e.ClickedItem
private void GridView1_ItemClick(object sender, ItemClickEventArgs e)
{
int test = GridView1.Items.IndexOf(e.ClickedItem);
}
當我調試我的代碼,詮釋test
將始終具有價值-1
。我不知道爲什麼這不起作用,我希望你們中的一個人做。
編輯:感謝您的所有答覆!以下是一些額外信息:我已禁用SelectionMode
,因爲我只想捕獲ClickedItem
的索引。我想知道索引,因爲它與我的額外信息數組相關聯。
由於我的SelectionMode
設置爲'無',我無法捕獲SelectedItem/SelectedIndex。我試圖啓用它們,但又收到'-1'的值。
編輯#2:我沒有設置任何ItemSource
,因爲我手動添加項目。這可能是問題嗎?使用沒有ItemSource
我的ListView
沒有問題。
編輯#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"/>
可幫助http://www.telerik.com/community/forums/silverlight/gridview/ gridview-items-indexof-result-1.aspx –
我試過了,它適用於我,你如何綁定你的GridView? –
btw。你爲什麼需要一個索引? –