locked
Get Tapped Item GridView RRS feed

  • Question

  • Hello

    When i tap a item in a gridview, how can i get the tapped item?

    regards

    Dieter

    Monday, September 10, 2012 9:15 AM

Answers

  • you should use ItemClick event.

    • Marked as answer by Aaron Xue Monday, September 24, 2012 9:40 AM
    Monday, September 10, 2012 11:21 AM
  • Hi,

    We can't get the Clicked item via Tapped event directly. We can get the Y coordinate and calculate the item index we clicked then get the item.

    Here is an example:

            private void GridView_Tapped_1(object sender, TappedRoutedEventArgs e)
            {
                int item = 0;
                Double coY = e.GetPosition((UIElement)sender).Y;
                Debug.WriteLine(coY.ToString());
    
                GridView gv = sender as GridView;
                if (sender is GridView)
                {
                    gv.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                    Size gvSize = gv.DesiredSize;
                    item = (int)(coY / gvSize.Height * gv.Items.Count);
                    item = item > gv.Items.Count ? gv.Items.Count : item;
                }
    
                Debug.WriteLine(item);
    
                Debug.WriteLine(gv.Items[item]);
    
            }
     

    Actually, I think using ItemClick event would be a better solution, because we can get the clicked item much easier.

    Hope this helps

     


    Aaron Xue [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Aaron Xue Monday, September 24, 2012 9:40 AM
    Tuesday, September 11, 2012 7:51 AM

All replies

  • e.ClickedItem? , where e is the EventAgrs of the eventhandler.


    Monday, September 10, 2012 9:51 AM
  • nops, it's a TappedRoutedEventArgs , but no ClickedItem
    Monday, September 10, 2012 11:13 AM
  • you should use ItemClick event.

    • Marked as answer by Aaron Xue Monday, September 24, 2012 9:40 AM
    Monday, September 10, 2012 11:21 AM
  • Hi,

    We can't get the Clicked item via Tapped event directly. We can get the Y coordinate and calculate the item index we clicked then get the item.

    Here is an example:

            private void GridView_Tapped_1(object sender, TappedRoutedEventArgs e)
            {
                int item = 0;
                Double coY = e.GetPosition((UIElement)sender).Y;
                Debug.WriteLine(coY.ToString());
    
                GridView gv = sender as GridView;
                if (sender is GridView)
                {
                    gv.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                    Size gvSize = gv.DesiredSize;
                    item = (int)(coY / gvSize.Height * gv.Items.Count);
                    item = item > gv.Items.Count ? gv.Items.Count : item;
                }
    
                Debug.WriteLine(item);
    
                Debug.WriteLine(gv.Items[item]);
    
            }
     

    Actually, I think using ItemClick event would be a better solution, because we can get the clicked item much easier.

    Hope this helps

     


    Aaron Xue [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Aaron Xue Monday, September 24, 2012 9:40 AM
    Tuesday, September 11, 2012 7:51 AM
  • I have another solution for this.

    Use GotFocus event along with DOubleTap event.

    As the two will be called in succession , you will get which item generated the Doubletap.

    private void OnFocus(object sender, Windows.UI.Xaml.RoutedEventArgs e)

    {

    ListViewItem lv = (ListViewItem) e.OriginalSource;

    str = lv.Content.ToString(); //you can use "str" in your DoubleTap event handler

    }

    Thanks.


    recieved alert

    Friday, October 18, 2013 2:48 PM