Johann
Johann

Reputation: 523

ItemClick event on part of GridView

I have a collection of objects that I want to bind to my Xaml (using a GridView at the moment). I don't know if a GridView is the best tool for what I want to achieve.

This is roughly my current DataTemplate:

data template

I need a clicked/tapped trigger on Grid1. But when I use this template with a GridView, the ItemClick event will get triggered on the complete template - which is correct of course. How can I set an click event on Grid1? Can I add this event in my DataTemplate declaration?

That's basically the xaml for my GridView:

<GridView x:Name="RoomsGridView"
                    VerticalAlignment="Top"
                    ItemsSource="{Binding Rooms}"
                    ItemTemplate="{StaticResource RoomTemplate}"
                    SelectionMode="None"
                    IsSwipeEnabled="false" 
                    IsItemClickEnabled="True"
                    ItemClick="RoomsGridView_ItemClick" Padding="0">
                        <GridView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal"/>
                            </ItemsPanelTemplate>
                        </GridView.ItemsPanel>                            
                    </GridView>

Edit: I've found this sample but this seems only to work with Buttons!?

Upvotes: 0

Views: 1944

Answers (1)

Kasper Holdum
Kasper Holdum

Reputation: 13353

In the code you posted above you've told to GridView to capture item clicks via IsItemClickEnabled="True". You cannot have that option enabled if you want to be able to click on elements inside the DataTemplate.

I would suggest that you disable IsItemClickEnabled and instead add a click handler to your Grid1. Even better would be to substitute your Grid1 with a button that has custom styling. This would further allow you to bind the button to a command that you can control from your view model.

Upvotes: 1

Related Questions