Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows 8 C# GridView e.ClickedItem

I've got a GridView with items and an array with details about specific Items in my GridView. I want to get the Index of the clicked item, I tried to use the same code I used earlier with a ListView:

private void GridView1_ItemClick(object sender, ItemClickEventArgs e)
{
    int test = GridView1.Items.IndexOf(e.ClickedItem);
}

When I debug my code, int test will always have the value -1. I have no clue why this is not working and I am hoping one of you guys do.

Edit: Thanks for all the replies! Here is some extra information: I have disabled the SelectionMode since I only want to capture the index of the ClickedItem. I want to know the index since it is linked to my array of extra information.

Since my SelectionMode is set to 'None' I am not able to capture SelectedItem/SelectedIndex. I tried to enable them, but received the value '-1' again.

Edit #2: I have not set any ItemSource since I add Items manually. Could this be the problem? My ListView had no problems using no ItemSource.

Edit #3:

C# code:

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"/>
like image 388
Zerfox Avatar asked Jul 20 '26 16:07

Zerfox


2 Answers

Dont know why you are doing this that way, it's much easier to declare controls in markup, and bind it in markup to some object poperty or similar.

But, to get index of item you should search for clicked item parent, something like this :

private void GridView1_ItemClick(object sender, ItemClickEventArgs e)
{
    int test = GridView1.Items.IndexOf((e.ClickedItem as FrameworkElement).Parent);
}
like image 113
Antonio Bakula Avatar answered Jul 24 '26 10:07

Antonio Bakula


-1 is when none index is selected. If You need to do something with your clicked item You can do this:

var item=GridView1.SelectedItem;

If You need only index you can do this:

var index= GridView1.SelectedIndex;
like image 45
Norbert Pisz Avatar answered Jul 24 '26 11:07

Norbert Pisz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!