Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView in UWP

I are facing issue with GridView Control. We had a working Windows Store App on 8.1 where GridView left and right mouse clicks had different functionality. In the case of left mouse click, we used to use “ItemClick” event which performs navigation to another XAML page. On right click of GridItem, it gets selected and shows the appbar, we have used “SelectionChanged” event for this.

Old Selected Item View in 8.1

We are now migrating our existing windows store app to UWP Application, we have used same gridView Code, we find significant difference in functionality and look & feel, we don’t see GridView Item Selected like above picture. We see “ItemClick” and “SelectionChanged” are working together. The flow is something like that on left click on the item, the control goes to SelectionChanged event and then ItemClick. We were not able to differentiate actions like Left Mouse Click and Right Mouse click, since both events are getting fired up upon clicking on left click/tapping. We have different functionality on left and right clicks of mouse.

Need help on how to mimic windows 8.1 functionality in UWP.

like image 315
Aakansha Avatar asked Oct 26 '15 20:10

Aakansha


1 Answers

My requirement was the I wanted to use Right Click/Long Tapped to select an item and take an action accordingly from App Bar Buttons and on Left Click/Tap should redirect me to the next XAML Page. The problem I was facing was the on Right Click, I wasnt able to detect that which items of GridView has been clicked and how can I add that into SelectedItem.

What I did was, I introduced extra Grid in DataTemplate of GridView. Within this Grid, I added RightTapped event.

The sample code snippet is

 <GridView x:Name="ItemGridView"   
              ItemsSource="{Binding Source={StaticResource ItemsViewSource}}" 
              IsItemClickEnabled="True" 
              SelectionMode="Single" ItemClick="ItemGridView_ItemClick" 
              SelectionChanged="ItemGridView_SelectionChanged">
        <GridView.ItemTemplate>
            <DataTemplate>
                <Grid RightTapped="Grid_RightTapped">
                    <Border Background="White"  BorderThickness="0" Width="210" Height="85">
                        <TextBlock Text="{Binding FileName}" />
                    </Border>
                </Grid>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>

The event name is Grid_RightTapped. This helped me detect that from which GridViewItem, I got the long tap/right click.

The code-behind for this is:

 private void Grid_RightTapped(object sender, RightTappedRoutedEventArgs e)
    {
        Song selectedItem = (sender as Grid).DataContext as Song;
        //the above line will get the exact GridViewItem where the User Clicked
        this.ItemGridView.SelectedItem = selectedItem;
        //the above line will add the item into SelectedItem and hence, I can take any action after this which I require
        }
    }

The reason we are doing this way is, because now we can add clicked item into the GridView SelectedItem using Right Click. Now in UWP, clicked items are added into SelectedItem using left click only. And with left click, I can navigate to another page using ItemClick event.

like image 175
Aakansha Avatar answered Sep 20 '22 16:09

Aakansha