Seperate click event of each item in GridView

DB-2710 61 Reputation points

I want to create a seperate click event for each ListViewItem and GridViewItem in UWP (C#).

Basically, I want to show a GridView first. Then when it's GridViewItem is clicked, it shows a ListView accordingly.

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Roy Li - MSFT 31,551 Reputation points Microsoft Vendor


    Welcome to Microsoft Q&A!

    First of all, there is no click event for the GridViewItem, there is no seperated click event for the GridViewItem. Generally, we will handle the GridView.SelectionChanged event when the click happens. If I understand you correctly, you want to do different works when different item is clicked and you does not know how to indicate which item is clicked, right?

    If so, you could try to set the Tag property of the GridViewItem and when the SelectionChanged event is fired, you could get the tag value of the item to know which item is clicked.

    Here is a simple code snippet that you could refer to:


     <GridView x:Name="MyGrid" MaxWidth="1000" MinWidth="332" HorizontalAlignment="Center" SelectionChanged="MyGrid_SelectionChanged" >  
                <GridViewItem Width="300" Height="100" Margin="16" Tag="1">  
                    <TextBlock Text="123"/>  
                <GridViewItem Width="300" Height="100" Margin="16"  Tag="2">  
                    <TextBlock Text="234"/>  
                <GridViewItem Width="300" Height="100" Margin="16"  Tag="3">  
                    <TextBlock Text="345"/>  

    Code behind:

    public sealed partial class MainPage : Page  
            public MainPage()  
            private void MyGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)  
                var gridViewItem = MyGrid.ContainerFromItem(MyGrid.SelectedItem) as GridViewItem;  
                string str = gridViewItem.Tag.ToString();  
                // passing string "str" in   
                // switch statement  
                switch (str)  
                    case "1":  
                        Debug.WriteLine("It is 1");  
                    case "2":  
                        Debug.WriteLine("It is 2");  
                    case "3":  
                        Debug.WriteLine("It is 3");  

    Thank you.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

0 additional answers

Sort by: Most helpful